java.time.Clock.withZone() Method Example



Description

The java.time.Clock.withZone() method returns a copy of this clock with a different time-zone.

Declaration

Following is the declaration for java.time.Clock.withZone() method.

public abstract Clock withZone(ZoneId zone)

Parameters

zone − the time-zone to use to convert the instant to date-time, not null.

Return Value

a clock based on this clock with the specified time-zone, not null.

Example

The following example shows the usage of java.time.Clock.withZone() method.

package com.tutorialspoint;

import java.time.Clock;
import java.time.ZoneId;

public class ClockDemo {
   public static void main(String[] args) {
   
      Clock clock = Clock.systemUTC();
      Clock clock1 = clock.withZone(ZoneId.systemDefault());
      System.out.println("Clock : " + clock.instant());
      System.out.println("Clock1 : " + clock1.instant());
   }
}

Let us compile and run the above program, this will produce the following result −

Clock : 2017-03-07T07:00:19.909Z
Clock1 : 2017-03-07T07:00:19.921Z
Advertisements