This post will discuss 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 data types. All these data types have a corresponding class in JDBC, and each of them extends java.util.Date. This post will discuss 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 the java.util package and the other present in the java.sql package. The java.sql.Date corresponds to SQL DATE containing the 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 actually 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 and above.

 
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 and above. We can create a 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 and above, 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 a TIMESTAMP datatype, which, unlike the DATE datatype, can store the time information.

Java provides java.sql.Timestamp class, which is a thin wrapper around java.util.Date which has ability to hold the SQL TIMESTAMP. It is 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

To store only time information in a database, almost all relational databases have TIME datatype, which only stores the time information. In Java, we have the java.sql.Time class that corresponds to SQL TIME and only contains information about an hour, minutes, seconds, and milliseconds. It is 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

That’s all about converting java.util Date to java.sql Date, Timestamp, and Time in Java.