Java Instant Class | atZone() Method with Example

Instant Class atZone() method: Here, we are going to learn about the atZone() method of Instant Class with its syntax and example.
Submitted by Preeti Jain, on May 21, 2020

Instant Class atZone() method

  • atZone() method is available in java.time package.
  • atZone() method is used to return ZonedDateTime to merge this Instant with the given zone.
  • atZone() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • atZone() method may throw an exception at the time of representing Instant.
    DateTimeException: This exception may throw when the given parameter couldn't adjust this Instant.

Syntax:

    public ZonedDateTime atZone(ZoneId zo);

Parameter(s):

  • ZoneId zo – represents the zone to merge with this Instant.

Return value:

The return type of this method is ZonedDateTime, it returns ZonedDateTime object that holds the value merged this Instant with the given zone.

Example:

// Java program to demonstrate the example 
// of atZone(ZoneId zo) method 
// of Instant

import java.time.*;
public class AtZoneOfInstant {
    public static void main(String args[]) {
        // Instantiates two Instant , two ZoneId
        Instant ins1 = Instant.parse("2006-04-03T05:10:15.00Z");
        Instant ins2 = Instant.parse("2007-06-05T10:20:30.00Z");
        ZoneId zo_1 = ZoneId.of("Africa/Accra");
        ZoneId zo_2 = ZoneId.of("Africa/Asmara");

        // Display ins1,ins2,zo_1
        // and zo_2
        System.out.println("Instant ins1 and ins2: ");
        System.out.println("ins1: " + ins1);
        System.out.println("ins2: " + ins2);
        System.out.println();
        System.out.println("ZoneId zo_1 and zo_2: ");
        System.out.println("zo_1 to add: " + zo_1);
        System.out.println("zo_2 to add: " + zo_2);

        System.out.println();

        // Here, this method merges this Instant(ins1)
        // with the given ZoneId(zo_1) i.e. here
        // we are adding zo_1 (Africa/Accra) with 
        // this Instant (ins1)
        ZonedDateTime zo_da_time = ins1.atZone(zo_1);

        // Display zo_da_time
        System.out.println("ins1.atZone(zo_1): " + zo_da_time);

        // Here, this method merges this Instant(ins2)
        // with the given ZoneId(zo_2) i.e. here
        // we are adding zo_2 (Africa/Asmara) with 
        // this Instant (ins2)
        zo_da_time = ins2.atZone(zo_2);

        // Display zo_da_time
        System.out.println("ins2.atZone(zo_2): " + zo_da_time);
    }
}

Output

Instant ins1 and ins2: 
ins1: 2006-04-03T05:10:15Z
ins2: 2007-06-05T10:20:30Z

ZoneId zo_1 and zo_2: 
zo_1 to add: Africa/Accra
zo_2 to add: Africa/Asmara

ins1.atZone(zo_1): 2006-04-03T05:10:15Z[Africa/Accra]
ins2.atZone(zo_2): 2007-06-05T13:20:30+03:00[Africa/Asmara]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.