Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert java.sql.date to java.time.LocalDateTime

Tags:

java

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

like image 819
Ferre12 Avatar asked Feb 23 '17 11:02

Ferre12


People also ask

Does Java sql date have time?

Its main purpose is to represent SQL DATE, which keeps years, months and days. No time data is kept. In fact, the date is stored as milliseconds since the 1st of January 1970 00:00:00 GMT and the time part is normalized, i.e. set to zero. Basically, it's a wrapper around java.

What is the difference between LocalDateTime and date in Java?

For example, the old Date class contains both date and time components but LocalDate is just date, it doesn't have any time part in it like "15-12-2016", which means when you convert Date to LocalDate then time-related information will be lost and when you convert LocalDate to Date, they will be zero.

How do I get time from LocalDateTime?

You can get the time from the LocaldateTime object using the toLocalTime() method. Therefore, another way to get the current time is to retrieve the current LocaldateTime object using the of() method of the same class. From this object get the time using the toLocalTime() method.


1 Answers

It was actually easier than I thought. This worked for me:

//java.sql.ResultSet result result.getTimestamp("value").toLocalDateTime() 
like image 167
Ferre12 Avatar answered Oct 13 '22 22:10

Ferre12