ChronoLocalDateTime atZone() method in Java with Examples

The atZone(ZoneId zone) method of ChronoLocalDateTime interface is used to combine this ChronoLocalDateTime with a time-zone whose ZoneId is given as parameter to create an ZonedDateTime object. This method takes ZoneId as a parameter and combines the time-zone with this ChronoLocalDateTime after operation returns a ChronoZonedDateTime object.

Syntax:

ChronoZonedDateTime<D> atZone(ZoneId zone)

Parameters: This method accepts one parameter zone which is the zone to combine to this ChronoLocalDateTime object. It should not be null.



Return Value: This method returns a ChronoZonedDateTime which is the combination of current zone of ChronoLocalDateTime and the zone passed as the parameter.

Below programs illustrate the ChronoLocalDateTime.atZone() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// ChronoLocalDateTime.atZone() method
  
import java.time.*;
import java.time.chrono.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ChronoLocalDateTime object
        ChronoLocalDateTime date
            = LocalDateTime.parse("2018-12-06T19:21:12");
  
        // print ChronoLocalDateTime Value
        System.out.println("ChronoLocalDateTime: "
                           + date);
  
        // create ZoneId object
        ZoneId zone = ZoneId.of("Europe/Paris");
  
        // apply atZone method of ChronoLocalDateTime class
        ChronoZonedDateTime result = date.atZone(zone);
  
        // print results
        System.out.println("ChronoZonedDateTime: "
                           + result);
    }
}

chevron_right


Output:

ChronoLocalDateTime: 2018-12-06T19:21:12
ChronoZonedDateTime: 2018-12-06T19:21:12+01:00[Europe/Paris]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// ChronoLocalDateTime.atZone() method
  
import java.time.*;
import java.time.chrono.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ChronoLocalDateTime object
        ChronoLocalDateTime date
            = LocalDateTime.parse("2018-12-06T19:21:12");
  
        // print ChronoLocalDateTime Value
        System.out.println("ChronoLocalDateTime: "
                           + date);
  
        // create ZoneId object
        ZoneId zone = ZoneId.of("Asia/Aden");
  
        // apply atZone method
        ChronoZonedDateTime result
            = date.atZone(zone);
  
        // print results
        System.out.println("ChronoZonedDateTime: "
                           + result);
    }
}

chevron_right


Output:

ChronoLocalDateTime: 2018-12-06T19:21:12
ChronoZonedDateTime: 2018-12-06T19:21:12+03:00[Asia/Aden]

Reference: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoLocalDateTime.html#atZone-java.time.ZoneId-



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.