Java Data Type How to - Convert java.util.Date to java.time.LocalDateTime








Question

We would like to know how to convert java.util.Date to java.time.LocalDateTime.

Answer

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
//from   www.  ja  va  2 s . c om
public class Main {

  public static void main(String[] args) {
    Date date = new Date();
    LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());

    System.out.println(localDateTime);
  }
}

The code above generates the following result.