How do I convert a java.sql.Date
returned from a JDBC database to a java.time.LocalDateTime
?

- 1,193
- 1
- 8
- 17
-
9Possible duplicate of [Convert between LocalDate and sql.Date](http://stackoverflow.com/questions/29750861/convert-between-localdate-and-sql-date) – BackSlash Feb 23 '17 at 11:17
-
5I already looked at that question. But I'm trying to convert an sql.Date to a LocalDateTime and not a LocalDate. – Ferre12 Feb 23 '17 at 11:23
-
Then the answer linked by @nobody can help (duplicate): [Converting between java.time.LocalDateTime and java.util.Date](http://stackoverflow.com/questions/19431234/converting-between-java-time-localdatetime-and-java-util-date). `java.sql.Date` is a subclass of `java.util.Date`, so you can cast it. – BackSlash Feb 23 '17 at 11:25
4 Answers
It was actually easier than I thought. This worked for me:
//java.sql.ResultSet result
result.getTimestamp("value").toLocalDateTime()

- 1,193
- 1
- 8
- 17
-
1This is a plausible answer and correct for <= Java 7, but you can skip transformations using the `hibernate-java8` library. Might be a better way nowadays. – Julio Villane Jan 23 '18 at 14:20
-
It is not only plausible, but correct and simplistic. However, LocalDateTime was in fact introduced in 1.8 (Java 8). Conversion to the joda-time precursor that used to be de facto standard (external) time library in earlier versions of Java was made on the receiving end, such as by use of the DateTime constructor. – Martin Dec 17 '19 at 22:05
-
What object type does the result here refer to? (There is no getTimestamp() method on java.sql.Date.) – Woodchuck Oct 20 '21 at 16:01
-
I don't have the code in question anymore but I assume java.sql.ResultSet https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html – Ferre12 Oct 21 '21 at 17:51
Working method:
public LocalDate convertToLocalDateViaMilisecond(Date dateToConvert) {
return Instant.ofEpochMilli(dateToConvert.getTime())
.atZone(ZoneId.systemDefault())
.toLocalDate();
}
Non-working build for java.sql.Date (because it throws UnsupportedOperationException for instant() method):
public LocalDate convertToLocalDateViaInstant(Date dateToConvert) {
return dateToConvert.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
}
Credits to baeldung for this answer on his site:

- 1,542
- 17
- 16
-
if (date instanceof java.sql.Date) date = new Date(date.getTime()); – Shakirov Ramil Jul 02 '21 at 09:09
A possible way is to first convert it to java.time.LocalDate and then to java.time.LocalDateTime
sqlDate.toLocalDate().atStartOfDay()
Also you can specify a specific time using atTime() function
sqlDate.toLocalDate().atStartOfDay(hour, miniute, second)

- 1,137
- 17
- 30
If you are using Java 8 and Hibernate 5 there is no need of converting from/to java.sql.Date, only trust on Java's magic.
The dependency you need to use is:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>5.1.0.Final</version>
</dependency>
In case you are using spring-boot there is no need to declare any extra dependency.
If you are using a previous version of Java the recommendation is to define a Converter like this one:
@Converter(autoApply = true)
public class LocalDateAttributeConverter implements AttributeConverter<LocalDate, Date> {
@Override
public Date convertToDatabaseColumn(LocalDate locDate) {
return (locDate == null ? null : Date.valueOf(locDate));
}
@Override
public LocalDate convertToEntityAttribute(Date sqlDate) {
return (sqlDate == null ? null : sqlDate.toLocalDate());
}
}
You can found more about the use of LocalDateTime in Java 8 in this link, and more about this kind of converters on this link.

- 994
- 16
- 28
-
1This is not really an answer to the question, but rather a suggestion of addition of a third party dependency. – Martin Sep 02 '19 at 09:48
-
@Martin, not necessarily, if you are using spring-boot you don't need new dependencies. And probably you need to question yourself if it's correct to use plain jdbc directly instead of jpa – Julio Villane Sep 03 '19 at 13:48
-
3Yeesh. This still isn't an answer to the question, but rather religion. There are several options to JPA, and sometimes JDBC without much abstraction is in fact superior to pulling heavy dependencies like Hibernate, regardless if they are implicitly or explicitly declared. I'm not the one to judge, but a question should be answered in its own context, not filtered by personal preference. Answers like these do not help the community understanding code, and especially not phrasings like "only trust on Java's magic". – Martin Sep 06 '19 at 14:04
-
Dear @Martin, I've read your reply only today, but thank you, you have expressed with few words my position about some Java "developers" that I've encountered in these years – Enrico Bianchi May 12 '22 at 22:21