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:
import java.time.*;
import java.time.chrono.*;
public class GFG {
public static void main(String[] args)
{
ChronoLocalDateTime date
= LocalDateTime.parse( "2018-12-06T19:21:12" );
System.out.println( "ChronoLocalDateTime: "
+ date);
ZoneId zone = ZoneId.of( "Europe/Paris" );
ChronoZonedDateTime result = date.atZone(zone);
System.out.println( "ChronoZonedDateTime: "
+ result);
}
}
|
Output:
ChronoLocalDateTime: 2018-12-06T19:21:12
ChronoZonedDateTime: 2018-12-06T19:21:12+01:00[Europe/Paris]
Program 2:
import java.time.*;
import java.time.chrono.*;
public class GFG {
public static void main(String[] args)
{
ChronoLocalDateTime date
= LocalDateTime.parse( "2018-12-06T19:21:12" );
System.out.println( "ChronoLocalDateTime: "
+ date);
ZoneId zone = ZoneId.of( "Asia/Aden" );
ChronoZonedDateTime result
= date.atZone(zone);
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
Share your thoughts in the comments
Please Login to comment...