Convert java.util Date to java.sql Date, Timestamp and Time in Java

In this post, we will see how to convert java.util Date to java.sql Date, Timestamp and Time in Java.


 

Prerequisite: Difference between java.util.Date and java.sql.Date

 
We know that Relational databases like Oracle, MySQL supports DATE, TIME and TIMESTAMP datatypes. All these datatypes have a corresponding class in JDBC and each of them extend java.util.Date. In this post, we will see how to convert java.util Date to these classes –

 

1. Convert java.util.Date to java.sql.Date

Java has two Date classes, one present in java.util package and the other one present in java.sql package. The java.sql.Date corresponds to SQL DATE datatype which contains year, month and day information while hour, minute, second and millisecond information is not present.

The conversion from java.util.Date to java.sql.Date is acually very simple in Java. The Constructor of java.sql.Date expects milliseconds elapsed since Epoch (1 January 1970 00:00:00 GMT) which can be obtained by using getTime() method of java.util.Date object.

 

Download   Run Code

Output:

java.util.Date : Thu Jan 24 21:53:32 GMT 2015
java.sql.Date : 2015-01-24

 
Starting with Java 8, we should prefer using Instant class from java.time package which is similar to java.util Date class but it gives nanosecond accuracy. java.time.LocalDateTime (date-time without a time-zone), java.time.ZonedDateTime(date-time with a time-zone), LocalDate (date without a time-zone) and LocalTime (time without a time-zone) were also introduced in Java 8.

 
Since SQL data type DATE is date-only, with no time and time-zone information, it is better to use java.time.LocalDate class rather than java.util.Date in Java 8. We can create LocalDate instance by getting today’s date according to a particular time zone.

Now to convert java.time.LocalDate to java.sql.Date, we can use valueOf() method, a recent addition in Java 8, that can obtain an instance of java.sql.Date from a java.time.LocalDate object.

 

Download   Run Code

Output:

java.time.LocalDate : 2015-01-25
java.sql.Date : 2015-01-25

 

2. Convert java.util.Date to java.sql.Timestamp

If we need to store both date and time information in a database, we have TIMESTAMP datatype which, unlike DATE datatype, can store the time informtion.

Java provides java.sql.Timestamp class which is a thin wrapper around java.util.Date which has ability to hold the SQL TIMESTAMP. It’s toString() method formats a timestamp in yyyy-mm-dd hh:mm:ss.fffffffff format, where ffffffffff indicates nanoseconds.

 
Here’s how we can convert java.util Date to java.sql Timestamp in Java:

 

Download   Run Code

Output:

java.util.Date : Fri Jan 25 05:51:26 GMT 2015
java.sql.Timestamp : 2015-01-25 05:51:26.177

 

3. Convert java.util.Date to java.sql.Time

In order to store only time information in a database, almost all relational database have TIME datatype which only stores the time informtion. In java, we have java.sql.Time class that corresponds to SQL TIME and only contain information about hour, minutes, seconds and milliseconds. It’s toString() method outputs time in hh:mm:ss format.

 
Here’s how we can convert java.util Date to java.sql Time in Java:

 

Download   Run Code

Output:

java.util.Date : Fri Jan 25 05:51:26 GMT 2015
java.sql.Time : 05:51:26

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz