Tuesday, July 27, 2021

Difference Between java.util.Date and java.sql.Date in Java - JDBC Interview Question

Difference between java.util.Date and java.sql.Date in Java or why does Java have java.sql.Date if there is already a Date class in java util package is a common java interview question. There is always confusion among many Java developers familiar with both java.util.Date and java.sql.Date. Though many of them already using java.sql.Date to interface with DATE type of any relational database like MySQL, Oracle, or any other database. In this Java tutorial, we will see some differences between java.sql.Date and java.util.Date will help to understand both of them better.


This Java tutorial is in continuation of our earlier post on java.util.Date like How to convert String to Date in Java and how to get current Date and Time value in GMT timezone.  

if you haven't read them already you may find them interesting and useful. If you like JDBC articles then check out some JDBC Performance tips and  Benefits of PreparedStatement in Java.




Difference between java.util.Date and java.sql.Date in Java

Here are a few differences in java.sql.Date and java.util.Date in Java in point format, if you any other difference between them which is worth noting then please post in comments :

1. As per Javadoc java.sql.Date is a thin wrapper around millisecond value which is used by JDBC to identify an SQL DATE type.

2. java.sql.Date just represent DATE without time information while java.util.Date represents both Date and Time information. This is the major difference why java.util.Date can not directly map to java.sql.Date.

3. In order to suppress time information and to confirm with the definition of ANSI SQL DATE type, the millisecond values used in java.sql.Date instance must be "normalized by setting the hours, minutes, seconds, and milliseconds to zero in the timezone with DATE instance is associated. In other words, all time-related information is removed from java.sql.Date class.



>

Why do you need java.sql.Date if there is already java.util.Date exits?

Difference between java.sql.Date and java.util.Date in Javaif you look above the difference between java.sql.Date and java.util.Date, you will realize that both of them are not the same. The date from util package has is a combination of date and time while the Date from SQL package only represents only Date part. 

To be precise Date contains the year, month, and day information while Time means hour, minute, and second information. java.util.Date contains all year, month, day, hour, minute, and second information.

 In fact java.sql.Time and java.sql.TimeStamp which represents TIME and TIMESTAMP type of SQL database is more close to java.util.Date, It extends java.util.DATE and if you are using java.util.DATE in your Class to represent DATE value it's better to use TIMESTAMP type in Database and java.sql.Time in JDBC or DAO code.

By the way, if you get this JDBC interview question right there are more interesting questions to comes as follow-up like :

What is difference between java.sql.Date, java.sql.Time and java.sql.Timestamp ?
How to convert java.sql.Date into java.util.Date and vice versa?

Better to prepare for these questions as well, I will try to post answers to these JDBC questions in some other post.

That's all on the difference between java.sql.Date and java.util.Date. In summary, SQL date represent only Date information (e.g. year, month, day) while util Date represents both date and time information. Other Java questions you may like


3 comments :

abhishek ringsia said...

Hi ,please correct this "java.sql.Time in JDBC or DAO code" line to "java.sql.Timestamp in JDBC or DAO code".

if you are using java.util.DATE in your Class to represent DATE value its better to use TIMESTAMP type in Database and java.sql.Time in JDBC or DAO code

javin paul said...

@abhishek, Yes, Date includes both date and time, if you need both then use java.sql.Timestamp, if you need just date part, then use java.sql.Date and if you need just time part then use java.sql.Time. The Timestamp is mapped to DATETIME data type in SQL Server, java.sql.Date is mapped to Date and similar types in other database.

Unknown said...

good explains...

Post a Comment