LocalDateTime to java.sql.Date in java 8?
Asked Answered
R

4

29

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.

Ritornello answered 14/7, 2017 at 12:10 Comment(2)
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: #6778310Disparity
Could you please tell explicitly, do you care about the time part or not? If you don't, why can't you convert to LocalDate first?Gascon
C
35

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());
Canicula answered 14/7, 2017 at 12:17 Comment(6)
java.Sql.Date does retain the time components.This solution causes the time part to be lost.Byword
@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.Canicula
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.Byword
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.Canicula
Instead of asking the OP to clarify the unclear question you are supplying it with a misleading answer. If you are also composing the answer using info about some fault in JDK, you should explain that in the answer in the first place, ideally providing some reference.Gascon
@user9999, comments here are not part of my answer, and there isn't anything unclear about the original question, as far as I'm concerned. If you think my answer is misleading - explain how and why.Canicula
M
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);
Motorboat answered 14/7, 2017 at 13:22 Comment(2)
This answer must be the correct answer of this question +1.Maciemaciel
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, and even calling toInstant() results in UnsupportedOperationException. These values only appear to be zeroes if you call the getTime()Canicula
B
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());
Byword answered 18/10, 2018 at 14:57 Comment(1)
It doesn't keep the time part.Foliage
P
1

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

new Date(Timestamp.valueOf(LocalDateTime.now()).getTime())
Pomerania answered 17/4, 2023 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.