June 25, 2022

Convert Instant to Java LocalDateTime, LocalTime

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

1. An Instant provides an instantaneous point of time in UTC (Coordinated Universal Time) so converting it to LocalDate, LocalDateTime involves converting instant to-

  • ZonedDateTime by specifying the ZoneID used.
  • OffsetDateTime by specifying the ZoneOffset used.

Once you have an instance of ZonedDateTime or OffsetDateTime you can get LocalDate, LocalTime or LocalDateTime by using methods toLocalDate(), toLocalTime(), toLocalDateTime() respectively.

2. Another way is to use ofInstant() static method in LocalDate, LocalTime, LocalDateTime classes for conversion.

Instant to LocalDate, LocalDateTime using ZonedDateTime

public class InsantExample {

  public static void main(String[] args) {
    Instant instant = Instant.parse("2019-10-20T02:35:15.245Z");
    System.out.println("Instant- " + instant);
    // Getting ZonedDateTime
    ZonedDateTime zdt = instant.atZone(ZoneId.of("Europe/Paris"));
    System.out.println("ZonedDateTime- " + zdt);
    // Getting LocalDate
    System.out.println("LocalDate- " + zdt.toLocalDate());		
    System.out.println("LocalTime- " + zdt.toLocalTime());		
    System.out.println("LocalDateTime- " + zdt.toLocalDateTime());
  }
}
Output
Instant- 2019-10-20T02:35:15.245Z
ZonedDateTime- 2019-10-20T04:35:15.245+02:00[Europe/Paris]
LocalDate- 2019-10-20
LocalTime- 04:35:15.245
LocalDateTime- 2019-10-20T04:35:15.245

Instant to LocalTime, LocalDateTime using OffsetDateTime

public class InsantExample {
  public static void main(String[] args) {
    Instant instant = Instant.parse("2019-10-20T02:35:15.245Z");
    System.out.println("Instant- " + instant);
    // Getting OffsetDateTime by specifying offset
    OffsetDateTime odt = instant.atOffset(ZoneOffset.of("+02:00"));
    System.out.println("OffsetDateTime- " + odt);
    // Getting LocalDate
    System.out.println("LocalDate- " + odt.toLocalDate());		
    System.out.println("LocalTime- " + odt.toLocalTime());		
    System.out.println("LocalDateTime- " + odt.toLocalDateTime());
  }
}
Output
Instant- 2019-10-20T02:35:15.245Z
OffsetDateTime- 2019-10-20T04:35:15.245+02:00
LocalDate- 2019-10-20
LocalTime- 04:35:15.245
LocalDateTime- 2019-10-20T04:35:15.245

Using ofInstant() method

For LocalDate
Instant instant = Instant.parse("2019-10-20T02:35:15.245Z");
System.out.println("Instant- " + instant);

LocalDate ld = LocalDate.ofInstant(instant, ZoneId.of("Asia/Kolkata"));
System.out.println("LocalDate- " + ld);
Output
Instant- 2019-10-20T02:35:15.245Z
LocalDate- 2019-10-20
For LocalTime
Instant instant = Instant.parse("2019-10-20T02:35:15.245Z");
System.out.println("Instant- " + instant);

LocalTime lt = LocalTime.ofInstant(instant, ZoneId.of("Asia/Kolkata"));
System.out.println("LocalTime- " + lt);
Output
Instant- 2019-10-20T02:35:15.245Z
LocalTime- 08:05:15.245
For LocalDateTime
Instant instant = Instant.parse("2019-10-20T02:35:15.245Z");
System.out.println("Instant- " + instant);

LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.of("Asia/Kolkata"));
System.out.println("LocalDateTime- " + ldt);
Output
Instant- 2019-10-20T02:35:15.245Z
LocalDateTime- 2019-10-20T08:05:15.245

That's all for the topic Convert Instant to Java LocalDateTime, LocalTime . 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