How to Convert an Instant to Java 8 ZonedDateTime

Java 8 ZonedDateTime is an immutable date-time object representing date and time with a time-zone in the ISO-8601 calendar system.

ZonedDateTime class provides the ZonedDateTime.ofInstant(instant) function to convert an Instant to Java 8 ZonedDateTime object.

In this tutorial, you will learn about converting Instant to Java 8 ZonedDateTime object in detail.

Convert Instant to ZonedDateTime

The ZonedDateTime class is a combination of LocalDateTime and a ZoneId to store all date and time fields to nanoseconds precision with a time-zone.

To convert an Instant to ZonedDateTime using .ofInstant(instant) method,

  • Convert the Instant to ZonedDateTime using the ZonedDateTime.ofInstant(instant, zoneId) method. Internally the ofInstant() method will convert Instant to Epoch seconds and nanoseconds while creating ZonedDateTime and if the Epoch value is not within the supported range then it will throw DateTimeException.

  • ZoneId can be assigned with system time-zone using ZoneId.systemDefault() or with custom time-zone using ZoneId.of(zoneId) method.

Code

The following example converts the current Instant time to a ZonedDateTime object with system time zone[Europe/London].

    import java.time.Instant;  
    import java.time.ZoneId;  
    import java.time.ZonedDateTime;  
    //main
    ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
    System.out.println(zonedDateTime);  //2023-01-29T17:45:13.465532100Z[Europe/London]

You can convert an Date to Instant and also assign custom time zone value like America/New_York, Pacific/Guadalcanal for converting to ZonedDateTime using .ofInstant(instant, zoneId) method.

        ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(1580304983000L), ZoneId.of("America/New_York"));
        System.out.println(zonedDateTime);  // 2020-01-29T08:36:23-05:00[America/New_York]

Convert ZonedDateTime to Instant

The Java 8 Instant represents the current point on a time line and it can be used to record event time-stamp in an application. Instant is stored as a long value representing epoch seconds and an int value representing nanoseconds(0-999,999,999).

To convert ZonedDateTime to Instant using .toInstant() method,

  • Invoke the ZonedDateTime.toInstant() to convert the ZonedDateTime to Instant. Internally the ZonedDateTime will be converted to epoch seconds & nanoseconds for creating Instant.

Code

The following code will convert an current ZonedDateTime value to Instant using .toInstant() method.

    import java.time.Instant;   
    import java.time.ZoneId;  
    import java.time.ZonedDateTime;    
    //main
    LocalDateTime localDateTime = LocalDateTime.now();  
    Instant instant = ZonedDateTime.of(localDateTime,ZoneOffset.UTC).toInstant();
    System.out.println(instant);  //2023-01-29T17:55:30.955509800Z

To convert an Instant to LocalDateTime in Java, check this tutorial How to convert an Instant to Java 8 LocalDateTime

Related Topics

Leave a Comment