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.
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.
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().
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().
baseclock Zone:Etc/UTC clockWithOtherZone Zone:Asia/Calcutta
- Java Clock tickMinutes() method in Java with Examples
- Clock tickSeconds() method in Java with Examples
- Java 8 Clock millis() Method with Examples
- Clock systemUTC() Method in Java with Examples
- Clock tick() Method in Java with Examples
- Java 8 Clock instant() method with Examples
- Java 8 Clock offset() method with Examples
- Java 8 Clock getZone() method with Examples
- Java 8 Clock hashCode() method with Examples
- Java 8 Clock equals() Method with Examples
- Java 8 Clock fixed() method with Examples
- Clock systemDefaultZone() Method in Java with Examples
- Clock system() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.reverse() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.