Java Program to convert java.util.Date to java.time.LocalDateTime

Java 8Object Oriented ProgrammingProgramming

First, set the date −

java.util.Date date = new Date();

Now, convert the above Date to java.time.LocalDateTime −

java.time.LocalDateTime dateTime =
   java.time.LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());

Example

import java.time.ZoneId;
import java.util.Date;
public class Demo {
   public static void main(String[] args) {
      java.util.Date date = new Date();
      System.out.println("Date = "+date);
      java.time.LocalDateTime dateTime =
         java.time.LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
      System.out.println("LocalDateTime = "+dateTime);
   }
}

Output

Date = Thu Apr 18 23:39:34 IST 2019
LocalDateTime = 2019-04-18T23:39:34.400
raja
Published on 26-Apr-2019 10:22:47
Advertisements