Java Data Type How to - Convert LocalDateTime to java.sql.Date








Question

We would like to know how to convert LocalDateTime to java.sql.Date.

Answer

import java.time.LocalDateTime;
import java.time.ZoneId;
//from www . j  av  a 2s .  c  o m
public class Main {

  public static void main(String[] args) {
    System.out.println(localTimeToDate(LocalDateTime.now()));
  }

  public static java.sql.Date localTimeToDate(LocalDateTime lt) {
    return new java.sql.Date(lt.atZone(ZoneId.systemDefault()).toInstant()
        .toEpochMilli());
  }
}

The code above generates the following result.