java.time.Clock.system(ZoneId zone)
The method java.time.Clock.system(ZoneId zone) a clock that uses the best available system clock in the specified zone – never null.
Method signature
The signature of the java.time.Clock.system(ZoneId zone) method is as follows:
public static Clock system(ZoneId zone)
The method is available since Java 8.
Parameters
- ZoneId zone: the time-zone that will be used to convert the instant to date-time, not null.
Return value
Returns a clock that uses the best available system clock in the specified zone, never null.
Exceptions
The method throws no exceptions.
Example usage
In the following code we use java.time.Clock.system(ZoneId zone):
package com.farenda.java.time; import java.time.Clock; import java.time.ZoneId; import java.util.Date; import java.util.concurrent.TimeUnit; public class ClockExample { public static void main(String[] args) { Date now = new Date(); System.out.println("Current date: " + now); ZoneId centralEurope = ZoneId.of("Europe/Warsaw"); Clock clock = Clock.system(centralEurope); System.out.println("From clock: " + new Date(clock.millis())); try { // wait for a while: TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Second later: " + new Date(clock.millis())); } }
The above code produces the following output:
Current date: Sat May 21 10:41:26 CEST 2016 From clock: Sat May 21 10:41:26 CEST 2016 Second later: Sat May 21 10:41:27 CEST 2016
References:
- Compare with Fixed Clock behavior