How to convert LocalDateTime
to java.sql.Date
in java-8?
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
.
How to convert LocalDateTime
to java.sql.Date
in java-8?
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
.
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());
sql.Date
, and obviously it doesn't care about retaining time. If we cared about that, we'd be converting to sql.Timestamp
. –
Canicula 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 @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);
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 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());
You can get java.sql.Date object from LocalDateTime with time, but it is not very pretty.
new Date(Timestamp.valueOf(LocalDateTime.now()).getTime())
© 2022 - 2024 — McMap. All rights reserved.