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

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.
  1. 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.
  1. 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.

Read more: http://javarevisited.blogspot.com/2012/04/difference-between-javautildate-and.html#ixzz3PV25UWPZ


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s