java.time.Instant.atZone() Method Example



Description

The java.time.Instant.atZone(ZoneId zone) method combines this instant with a time-zone to create a ZonedDateTime.

Declaration

Following is the declaration for java.time.Instant.atZone(ZoneId zone) method.

public ZonedDateTime atZone(ZoneId zone)

Parameters

zone − the zone to combine with, not null.

Return Value

the zoned date-time formed from this instant and the specified zone, not null.

Exceptions

DateTimeException − if the result exceeds the supported range.

Example

The following example shows the usage of java.time.Instant.atZone(ZoneId zone) method.

package com.tutorialspoint;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Set;

public class InstantDemo {
   public static void main(String[] args) {

      Instant instant = Instant.parse("2017-02-03T10:37:30.00Z");
      System.out.println(instant);  
      
      Set<String> zones = ZoneId.getAvailableZoneIds();
      
      ZoneId zone = ZoneId.of(zones.iterator().next());
      
      ZonedDateTime  date = instant.atZone(zone);
      System.out.println(date);  
   }
}

Let us compile and run the above program, this will produce the following result −

2017-02-03T10:37:30Z
2017-02-03T13:37:30+03:00[Asia/Aden]
Advertisements