Explain Codes LogoExplain Codes Logo

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

java
date-conversion
java-date-api
jdbc
Anton ShumikhinbyAnton Shumikhin·Dec 4, 2024
TLDR
java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime()); //Bingo! java.util.Date is now java.sql.Date

Transform a java.util.Date instance to a java.sql.Date one using getTime() method that returns the number of milliseconds since the epoch. The java.sql.Date is used in SQL Commands when working with dates. This is straightforward and efficient.

Understanding the java.util.Date to java.sql.Date conversion

Converting between Date classes is pertinent when working with databases. java.sql.Date is used for handling date data in SQL queries. An essential point to remember is that there is no implicit or explicit conversion, so we use the getTime() method from java.sql.Date to instantiate the time in milliseconds.

Dealing with Java 8 and JDBC 4.2+

If you live in the modern era of Java 8 and upwards, you might be more familiar with java.time.LocalDate. And good news, the JDBC 4.2+ drivers let you use these classes directly! Here's how you can convert it:

java.time.LocalDate localDate = utilDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); java.sql.Date sqlDateFromLocalDate = java.sql.Date.valueOf(localDate); //Just call me Marty McFly

Transitioning from old to new Date API

If you find yourself in a situation needing to go back to the future between the old and new date-time API, here's a solution:

java.util.Date utilDateNow = java.util.Date.from(Instant.now()); java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(utilDateNow.getTime()); //Feels like living in the future, right?

Here, a java.sql.Timestamp retains the exact-time details of the java.util.Date.

Case: Protecting time information

Beware of the midnight curfew! When you convert java.util.Date to java.sql.Date, the java.sql.Date will truncate the hours, minutes, and seconds info. It's like turning back into a pumpkin at midnight! But hey, if you only need the date part, this can actually be quite handy.

java.util.Date utilDate = new java.util.Date(); // The start of a wonderful evening... java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(utilDate.getTime()); // But alas, midnight approaches!

Just to be safe, stick to java.sql.Timestamp if you need date and time values.

Know your timezones

Keep in mind the timezone issue. java.sql.Date does not have timezone data. To prevent Vanishing Daylight Phenomena (yeah, timezone stuff can be spooky), you might want to adjust the date with java.util.Calendar.

Calendar calendar = Calendar.getInstance(); calendar.setTime(utilDate); calendar.set(Calendar.HOUR_OF_DAY, 0); //No time to waste calendar.set(Calendar.MINUTE, 0); //Minute by minute calendar.set(Calendar.SECOND, 0); //Time is ticking calendar.set(Calendar.MILLISECOND, 0); //Down to the last millisecond java.sql.Date sqlDateWithCalendar = new java.sql.Date(calendar.getTimeInMillis()); //Voila!

Nobody will vanish here. Guaranteed.