java-8java-timedatetime-conversion

LocalDateTime to java.sql.Date in java 8?


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.


Solution

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