Difference between java.util.Date and java.sql.Date in Java or why does Java has 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 developer 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 which 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 checkout some JDBC Performance tips and Benefits of PreparedStatement in Java.
Difference between java.util.Date and java.sql.Date in Java
Here are few differences on 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 represent both Date and Time information. This is the major differences why java.util.Date can not directly map to java.sql.Date.
3. In order to suppress time information and to confirm with 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 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 ?
if you look above difference between java.sql.Date and java.util.Date, you will realize that both of them are not same. Date from util package has is combination of date and time while Date from SQL package only represent only Date part. to be precise Date contains 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 its 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 of these JDBC questions in some other post.
That's all on 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 represent both date and time information. Other Java questions you may like
Tidak ada komentar:
Posting Komentar