How to convert java.util.Date to java.sql.Timestamp?

You can convert a java.util.Date to java.sql.Timestamp value by using the getTime() method of Date class. This method return the long millisecond value from Epoch (1st January 1970 midnight) which you can pass to java.sql.Timestamp to create a new instance of Timestamp object in JDBC. Remember, java.sql.TimeStamp class is a wrapper around java.util.Date to allow JDBC to view it as SQL TIMESTAMP value. Only way to create a Timestamp instance is by passing the long time value because the second constructor of Timestamp class, which accepts individual fields e.g. year, month, date, hour, minute, second and nano is deprecated. Timestamp class can also hold up-to nano second value. 

Even though, it's a wrapper around Date class and contains both date and time fields it cannot be used in place of Date because it doesn't honor equals() contract i.e. when you compare Date with Timestamp it returns true but it return false for same object if you compare Timestamp to Date.

In other words, two instance of Date and Timestamp with same millisecond value will return true if you compare them using equals() method of Date class i.e. date.equals(timestamp) will return true. But, when you change the order i.e. compare them using Timestamp's equals() method e.g. timestamp.equals(date) the result will be false. This is against the symmetry property of equals contract which says if x.equals(y) is true then y.equals(x) should also be true.

This difference comes due to many factor and one of them is additional nano second precision which Timestamp need to support. You can see my earlier article about why you cannot use Timestamp in place of Date in Java to learn more about this interesting topic.

In this tutorial, you will learn how to convert a java.util.Date to java.sql.TimeStamp and vice-versa.




java.util.Date to java.sql.Timestamp and vice-versa

Here is the code to convert a java.util.Date instance to java.sql.TimeStamp object in JDBC. The code is using the same technique which we have used in past to convert java.util.Date to java.sql.Date i.e. extracting the long millisecond value from Date and using it create instance of Timestamp class

Date now = new Date();
Timestamp ts = new Timestamp(now.getTime());


And here is code for opposite side i.e. converting a java.sql.Timestamp value to java.util.Date in Java:

Timestamp now = new Timestamp();
Date date = new Date(now.getTime());

You can see it's quite simple, nothing complicated, just a couple of line of code and you are done. Though, if you want to learn more about date and time classes in JDBC, I suggest you to read Core Java Volume 2 - Advanced Features by Cay S. Horstmann. This book explains all important JDBC concepts in simple language.




Difference between java.util.Date and java.sql.TimeStamp

As I told you before, the java.sql.Timestamp is not exactly same as java.util.Date object, because its a composite of java.util.Date and a separate nanoseconds value. Only integral seconds are stored in the java.util.Date component, fractional seconds i.e. nanos are separate. This means Timestamp.equals(date) may return false if you pass a Date which is equal in value. This is the sole reason why Timestamp cannot be used in place of Date in JDBC.

This class should only be used while reading DATETIME values from database e.g. SQL Server. You can then convert that column into a Timestamp object in DAO layer and finally into a java.util.Object in your application layer.

If you want to learn more about Timestamp class in java.sql package and how to use it while interacting with database from Java application, please see Java : How to Program by Deitel and Deitel, one of the rare complete book for Java programmers.

How to convert java.util.Date to java.sql.Timestamp?



Important points

1) The java.sql.Timestamp is equivalent of SQL TIMESTAPM type in Java. It also maps to Types.TIMESTAMP

2) The setTimestamp() method is used to store Timestamp value from Java to Database.

3) The java.sql.TimeStamp method extends java.util.Date, but its composite of Date + nano second value

4) It's possible that date.equals(timestamp) == true but timestamp.equals(date) == false because equals() method of Timestamp class is not symmetric, hence violates equals contract.

5) The hashCode() method of java.sql.Timestamp class uses the underlying java.util.Date implementation and therefore does not include nanos in its computation.

6) Similar to Hashtable which doesn't follow camel case, Timestamp also has small 's' i.e. doesn't follow camel case so, don't write TimeStamp.


That's all about how to convert a java.util.Date to java.sql.TimeStamp value in JDBC. Remember, even though java.sql.Timestamp extends java.util.Date , its not a type inheritance, in-fact its just an implementation inheritance. Timestamp class also violates Liskov Substitution principle hence you cannot use Timestamp in place of Date in Java.



No comments :

Post a Comment