Java Convert LocalDateTime to Instant
Tags: Java LocalDateTime Java Instant Java 8
In this Java core tutorial we learn how to convert a java.time.LocalDateTime object into java.time.Instant object in Java programming language.
How to convert LocalDateTime to Instant in Java
In Java, to convert a LocalDateTime object to Instant object we can firstly convert it to ZonedDateTime object and call the toInstant() method to get Instant value as following Java code.
ConvertLocalDateTimeToInstantExample1.java
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class ConvertLocalDateTimeToInstantExample1 {
public static void main(String... args) {
LocalDateTime localDateTime = LocalDateTime.now();
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
System.out.println("LocalDateTime: " + localDateTime);
System.out.println("Instant: " + instant);
}
}
LocalDateTime: 2022-04-24T13:21:40.209901200
Instant: 2022-04-24T06:21:40.209901200Z
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 Date
Java Compare two LocalDateTime Values
Java Convert LocalDateTime to Epoch Seconds
Java Convert Epoch Seconds to LocalDateTime
Java Compare two LocalTime Values