Convert java.util Date to java.sql Date, Timestamp, and Time in Java
This post will discuss how to convert java.util Date to java.sql Date, Timestamp, and Time in Java.
Prerequisite:
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Main { // Method to convert `java.util.Date` to `java.sql.Date` in Java public static java.sql.Date convert(java.util.Date date) { return new java.sql.Date(date.getTime()); } public static void main(String[] args) { java.util.Date utilDate = new java.util.Date(); System.out.println("java.util.Date : " + utilDate); java.sql.Date sqlDate = convert(utilDate); System.out.println("java.sql.Date : " + sqlDate); } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Main { // Method to convert java.time.LocalDate to java.sql.Date in Java public static java.sql.Date convert(java.time.LocalDate localDate) { return java.sql.Date.valueOf(localDate); } public static void main(String[] args) { java.time.LocalDate todayLocalDate = java.time.LocalDate.now( java.time.ZoneId.of("Asia/Kolkata")); System.out.println("java.time.LocalDate : " + todayLocalDate); java.sql.Date sqlDate = convert(todayLocalDate); System.out.println("java.sql.Date : " + sqlDate); } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Main { // Method to convert `java.util.Date` to `java.sql.Timestamp` in Java public static java.sql.Timestamp convert(java.util.Date date) { return new java.sql.Timestamp(date.getTime()); } public static void main(String[] args) { java.util.Date utilDate = new java.util.Date(); System.out.println("java.util.Date : " + utilDate); java.sql.Timestamp sqlTimeStamp = convert(utilDate); System.out.println("java.sql.Timestamp : " + sqlTimeStamp); } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Main { // Method to convert `java.util.Date` to `java.sql.Time` in Java public static java.sql.Time convert(java.util.Date date) { return new java.sql.Time(date.getTime()); } public static void main(String[] args) { java.util.Date utilDate = new java.util.Date(); System.out.println("java.util.Date : " + utilDate); java.sql.Time sqlTime = convert(utilDate); System.out.println("java.sql.Time : " + sqlTime); } } |
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.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)