Java Clock withZone() method in Java with Examples

The java.time.Clock.withZone(ZoneId zone) method is a method of Clock class which returns a clock copy of clock object on which this method is applied, with a different time-zone. If there is a clock and it is required to change the zone of clock but not other properties, then withZone() method is used. This method takes zone as parameter which is the time-zone in which it is required to change. It returns the clock with zone, same as passed zone in parameter.

Syntax:

public abstract Clock withZone(ZoneId zone)

Parameter: This method takes a mandatory parameter zone of type ZoneId, in which it is required to change the time-zone.

Returns: This method returns a clock copy of clock object on which this method is applied, with a different time-zone passed as parameter.

Example:



Code:
//Clock with default zone
Clock clock1=Clock.systemUTC();
ZoneId zone = ZoneId.of("Asia/Calcutta");
Clock clock2 = clock1.withZone(zone);
System.out.println(clock2.toString());

Output:: 
SystemClock[Asia/Calcutta]

Explanation:: 
when withZone() is called for Clock object clock1 with zoneId "Asia/Calcutta",
then the withZone() method will return a Clock whose Zone is "Asia/Calcutta".

Below programs illustrates withZone() method of java.time.Clock class:

Program 1: To create a clock with similar properties as First Clock but zoneId equal to “Asia/Calcutta” with the help of withZone().

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// withZone() method of Clock class
  
import java.time.*;
  
// create class
public class withZoneMethodDemo {
  
    // Main method
    public static void main(String[] args)
    {
        // create a base Clock with systemUTC() method
        Clock baseclock = Clock.systemUTC();
  
        // get ZonedDateTime object from baseclock
        // clock instant to get date time
        ZonedDateTime baseTime = baseclock
                                     .instant()
                                     .atZone(baseclock.getZone());
  
        // print details of ZonedDateTime
        System.out.println("ZonedDateTime of baseclock "
                           + baseTime.toString());
  
        // create ZoneId object with Zone Asia/Calcutta
        ZoneId zone = ZoneId.of("Asia/Calcutta");
  
        // apply withZone() to change zone from utc to Asia/Calcutta
        Clock clockWithOtherZone = baseclock.withZone(zone);
  
        // get ZonedDateTime object from clockWithOtherZone
        // clock instant to get date time
        ZonedDateTime calcuttaTime = clockWithOtherZone
                                         .instant()
                                         .atZone(clockWithOtherZone.getZone());
  
        // print details of ZonedDateTime
        System.out.println("ZonedDateTime of clockWithOtherZone "
                           + calcuttaTime.toString());
    }
}

chevron_right


Output:

ZonedDateTime of baseclock 2018-08-24T08:09:17.354Z
ZonedDateTime of clockWithOtherZone 2018-08-24T13:39:17.539+05:30[Asia/Calcutta]

Program 2: Print the zoneId using getZone() for clock created by withZone().

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// withZone() method of Clock class
  
import java.time.*;
  
// create class
public class withZoneMethodDemo {
  
    // Main method
    public static void main(String[] args)
    {
        // create a base Clock with systemDefaultZone() method
        Clock baseclock = Clock.systemDefaultZone();
  
        // print details of ZonedDateTime
        System.out.println("baseclock Zone:"
                           + baseclock.getZone());
  
        // create ZoneId object with Zone Asia/Calcutta
        ZoneId zone = ZoneId.of("Asia/Calcutta");
  
        // apply withZone() to change zone
        // of baseclock to Asia/Calcutta
        Clock clockWithOtherZone = baseclock.withZone(zone);
  
        // print details of ZonedDateTime
        System.out.println("clockWithOtherZone Zone:"
                           + clockWithOtherZone.getZone());
    }
}

chevron_right


Output:

baseclock Zone:Etc/UTC
clockWithOtherZone Zone:Asia/Calcutta

Reference:
https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#withZone-java.time.ZoneId-

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




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.