Java Program to convert LocalDateTime to java.util.Date

Java 8Object Oriented ProgrammingProgramming

Set the LocalDateTime to current datetime −

LocalDateTime dateTime = LocalDateTime.now();

Create an Instant −

Instant i = dateTime.atZone(ZoneId.systemDefault()).toInstant();
Convert LocalDateTime to java.util.Date:
java.util.Date date = Date.from(i);

Example

 Live Demo

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

Output

Instant = 2019-04-19T04:34:31.271973Z
Date = Fri Apr 19 10:04:31 IST 2019
raja
Published on 23-Apr-2019 12:18:40
Advertisements