June 25, 2022

Convert LocalDate, LocalTime, LocalDateTime to Java Instant

In this post we’ll see Java programs to convert LocalDate, LocalTime and LocalDateTime to java.time.Instant.

Convert LocalDate to Instant in Java

An Instant provides an instantaneous point of time in UTC (Coordinated Universal Time) so converting LocalDate to instant requires adding time to LocalDate.

In LocalDate class there is a method atStartOfDay(ZoneId zone) which returns a ZonedDateTime from this LocalDate at the earliest valid time as per the time-zone rules for the passed ZoneId.

Once you have a ZonedDateTime instance you can call toInstant() method on that to get the Instant.

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class InsantExample {
  public static void main(String[] args) {
    LocalDate localDate = LocalDate.parse("2020-07-29");
    ZonedDateTime zdt = localDate.atStartOfDay(ZoneId.systemDefault());
    System.out.println("Zoned date time- " + zdt);
    Instant instant = zdt.toInstant();
    System.out.println("Instant- " + instant);
  }
}

Convert LocalTime to Instant in Java

If you have a LocalTime then you have to add date to it first to get a LocalDateTime for that you can use atDate() method of LocalTime.

By adding time-zone information to the LocalDateTime you can get a ZonedDateTime and then convert that to Instant.

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;

public class InsantExample {
  public static void main(String[] args) {
    LocalTime time = LocalTime.of(10, 53);
    LocalDateTime ldt = time.atDate(LocalDate.parse("2020-07-29"));
    Instant instant = ldt.atZone(ZoneId.systemDefault()).toInstant();
    System.out.println("Instant- " + instant);
  }
}

Convert LocalDateTime to Instant in Java

LocalDateTime has both date and time information, in order to convert LocalDateTime to Instant you just need to add time-zone information. That can be done in following ways.

Add ZoneId to the LocalDateTime and covert it to ZonedDateTime.

LocalDateTime ldt = LocalDateTime.now(); 
ZonedDateTime zdt = ldt.atZone(ZoneId.systemDefault());
System.out.println("Instant- " + zdt.toInstant());

By using toInstant(ZoneOffset offset) method-

LocalDateTime ldt = LocalDateTime.now(); 
ZoneId zone = ZoneId.of("Asia/Kolkata"); 
System.out.println("Instant- " + ldt.toInstant(zone.getRules().getOffset(ldt)));

Convert ZonedDateTime to Instant in Java

Since ZonedDateTime has all the information; date, time and time-zone just use toInstant() method to convert ZonedDateTime to Instant.

ZonedDateTime zdt = ZonedDateTime.now();
System.out.println("Instant- " + zdt.toInstant());

That's all for the topic Convert LocalDate, LocalTime, LocalDateTime to Java Instant. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment