Monday, September 6, 2021

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

You can convert a java.util.Date to java.sql.Timestamp value by using the getTime() method of Date class. This method returns 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. The only way to create a Timestamp instance is by passing the long time value because the second constructor of the Timestamp class, which accepts individual fields like a year, month, date, hour, minute, second, and nano is deprecated. Timestamp class can also hold up to nanosecond 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 returns false for the same object if you compare Timestamp to Date.

In other words, two instances of Date and Timestamp with the same millisecond value will return true if you compare them using the 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 like 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 factors and one of them is additional nanosecond precision which Timestamp needs 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. Btw, if you are new to JDBC and looking for a comprehensive online course to learn JDBC in-depth then I also suggest you check out the Complete JDBC Programming course on Udemy. It's a great course of direct classroom lectures and covers JDBC in depth




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 the past to convert java.util.Date to java.sql.Date i.e. extracting the long millisecond value from Date and using it to create an instance of Timestamp class

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


And here is code for the 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 lines of code, and you are done. Though, if you want to learn more about data and time classes in JDBC, I suggest you join these online advanced core Java courses to learn about advanced concepts. This also contains JDBC courses to 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 that 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 databases e.g. SQL Server. You can then convert that column into a Timestamp object in the DAO layer and finally into a java.util.Object in your application layer.

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

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



Important points

1) The java.sql.Timestamp is equivalent to SQL TIMESTAMP 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 the camel case, Timestamp also has small 's' i.e. doesn't follow the 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 it's just an implementation inheritance. Timestamp class also violates the Liskov Substitution principle hence you cannot use Timestamp in place of Date in Java.


Other JDBC Date and Time articles you may like
  • Difference between java.util.Date and java.sql.Date in JDBC? (answer)
  • Difference between java.sql.Time, java.sql.Timestamp, and java.sql.Date in JDBC? (answer)
  • How to convert java.util.Date to java.sql.Date in JDBC? (example)
  • How to convert java.sql.Date to java.util.Date in JDBC? (example)
  • How to convert java.util.Date to java.sql.Timestamp in JDBC? (example)
  • Why is String class made final in Java? (answer)
  • Why Hibernate Entity class should not be final in Java? (answer)
  • Why Timestamp cannot be used in place of Date in Java? (answer)
Thanks for reading this article so far. If you find this article and example useful then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note. 


No comments:

Post a Comment