LocalDateTime to java.sql.Date in java 8?

Ajeetkumar picture Ajeetkumar · Jul 14, 2017 · Viewed 38.1k times · Source

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.

Answer

M. Prokhorov picture M. Prokhorov · Jul 14, 2017

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());