28

How to convert LocalDateTime to java.sql.Date in ?

My search on internet mostly give me Timestamp related code or LocalDate to java.sql.Date. I'm looking for LocalDateTime to java.sql.Date.

Ajeetkumar
  • 1,271
  • 7
  • 16
  • 34
  • I think there is a misconception, due if you have Date and Time in LocalDateTime the natural conversion is to java.sql.Timestamp because it's supposed that you column have this information or will have. While if you have only Date in LocalDate the conversion will be to java.sql.Date. Of couse in my humble opinion. You can see more here: https://stackoverflow.com/questions/6777810/a-datetime-equivalent-in-java-sql-is-there-a-java-sql-datetime – pazfernando Jan 22 '22 at 22:11

4 Answers4

34

There is no direct correlation between LocalDateTime and java.sql.Date, since former is-a timestamp, and latter is-a Date.

There is, however, a relation between LocalDate and java.sql.Date, and conversion can be done like this:

LocalDate date = //your local date
java.sql.Date sqlDate = java.sql.Date.valueOf(date)

Which for any given LocalDateTime gives you the following code:

LocalDateTime dateTime = // your ldt
java.sql.Date sqlDate = java.sql.Date.valueOf(dateTime.toLocalDate());
M. Prokhorov
  • 3,894
  • 25
  • 39
  • 19
    java.Sql.Date does retain the time components.This solution causes the time part to be lost. – Andy Thomas Oct 18 '18 at 15:05
  • 4
    @AndyThomas, this question is about converting DateTime to `sql.Date`, and obviously it doesn't care about retaining time. If we cared about that, we'd be converting to `sql.Timestamp`. – M. Prokhorov Oct 19 '18 at 11:35
  • 1
    https://docs.oracle.com/javase/6/docs/api/java/sql/Date.html - this class supports time components (albeit deprecated), so obviously it can/does care about retaining time. – Andy Thomas Oct 19 '18 at 15:56
  • 1
    Once you call `getHours` on a `sql.Date`, you might rethink whether it has time or not. There is *no* time components there. The fact that it inherits from `java.util.Date` is a very unfortunate error on the part of JDK developers. – M. Prokhorov Oct 22 '18 at 11:53
30

@M. Prokhorov's answer is correct, I just want to add a few points.

A java.sql.Date keeps only the day, month and year values. The time values (hour, minute, seconds and milliseconds) are all set to zero. So, when converting a LocalDateTime to a java.sql.Date, these fields are lost.

If you're doing a one-way conversion and don't mind losing those fields, then it's ok to do it:

LocalDateTime dt = // LocalDateTime value
// convert to Date (time information is lost)
java.sql.Date date = java.sql.Date.valueOf(dt.toLocalDate());

But if you want to restore the original LocalDateTime later, it's better to save the time fields separetely, so you can recover it:

LocalDateTime dt = // your LocalDateTime
// save time information (hour, minute, seconds, fraction of seconds)
LocalTime savedTime = dt.toLocalTime();
// convert to Date (time information is lost)
java.sql.Date date = java.sql.Date.valueOf(dt.toLocalDate());

// retrieve back the LocalDate (only day/month/year)
LocalDate localDate = date.toLocalDate();
// retrieve the LocalDateTime, with the original time values
LocalDateTime ldt = localDate.atTime(savedTime);
  • 1
    This answer must be the correct answer of this question +1. – cihan adil seven Oct 03 '17 at 12:31
  • Time values in `java.sql.Date` are not set to zero - there are no such values at all. In OpenJDK, calling `getHours()` or `setSeconds(v)` on a `java.sql.Date` results in [IllegalArgumentException](https://github.com/openjdk/jdk/blob/27f4b27138fcf0a06a76bdf8365ef5fbd07d3bab/src/java.sql/share/classes/java/sql/Date.java#L202), and even calling `toInstant()` results in [UnsupportedOperationException](https://github.com/openjdk/jdk/blob/27f4b27138fcf0a06a76bdf8365ef5fbd07d3bab/src/java.sql/share/classes/java/sql/Date.java#L316). These values only appear to be zeroes if you call the `getTime()` – M. Prokhorov Apr 12 '21 at 16:11
3

It is possible to convert from LocalDateTime to java.sql.date while retaining the time part without havng to make assumptions about the time-zone by using java.util.Date as an intermediary:

LocalDateTime dateValue = // your LocalDateTime
java.util.Date utilDate;
String dateFormat = "yyyy-MM-dd'T'HH:mm:ss";
DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern(dateFormat);
SimpleDateFormat sdf1 = new SimpleDateFormat(dateFormat);
try {
    utilDate = sdf1.parse(dateValue.format(dtf1));
} catch (ParseException e) {
    utilDate = null; // handle the exception
}
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
Andy Thomas
  • 1,367
  • 2
  • 14
  • 30
0

You can get java.sql.Date object from LocalDateTime with time, but it is not very pretty.

new Date(Timestamp.valueOf(LocalDateTime.now()).getTime())