Thursday, July 4, 2024
HomeLanguagesJavaChronoLocalDateTime atZone() method in Java with Examples

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:




// 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-

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments