Java Convert LocalDateTime to Date
Tags: Java LocalDateTime Java Date Java 8
In this Java core tutorial we learn how to convert a java.time.LocalDateTime object into java.util.Date object in Java programming language.
How to convert LocalDateTime to Date in Java
In Java to convert a LocalDateTime object to Date we can follow the steps below.
- First step, convert the LocalDateTime to Instant object.
- Second step, create new Date object from the above Instant.
ConvertLocalDateTimeToDateExample1.java
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class ConvertLocalDateTimeToDateExample1 {
public static void main(String... args) {
LocalDateTime localDateTime = LocalDateTime.now();
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
Date date = Date.from(instant);
System.out.println("LocalDateTime: " + localDateTime);
System.out.println("Date: " + date);
}
}
LocalDateTime: 2022-04-24T13:21:05.008692700
Date: Sun Apr 24 13:21:05 ICT 2022
Happy Coding 😊
Related Articles
Java Convert LocalDateTime to String
Java Convert LocalDateTime to LocalDate
Java Convert LocalDateTime to LocalTime
Java Convert LocalDateTime to OffsetDateTime
Java Convert LocalDateTime to ZonedDateTime
Java Convert LocalDateTime to Calendar
Java Convert LocalDateTime to Instant
Java Convert LocalDateTime to Epoch Seconds
Java Convert Epoch Seconds to LocalDateTime
Java Compare two LocalDateTime Values
Java Compare two LocalTime Values