Open In App

ChronoLocalDateTime atZone() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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);
    }
}


Output:

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

Program 2:




// 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);
    }
}


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-



Last Updated : 30 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads