Java Clock
May 07, 2019
This page will walk through Java Clock
example. Clock
belongs to java.time
package. Clock
is available since Java 8. Clock
provides access to the current instant, date and time using time-zone and best available system clock. We can use Clock
in place of using System.currentTimeMillis()
and TimeZone.getDefault()
methods. Clock
provides different ways to get current instant, date and time. For Example, instant()
method returns the Instant
. systemDefaultZone
gives current instant using default time-zone. systemUTC()
returns current instant using UTC time-zone. system
gives the clock for the specified time-zone. offset
give the clock that returns instant with the specified duration added. tick
gives the clock that returns instant from the specified clock truncated to the nearest occurrence of the specified duration.
Now here on this page we will provide examples for most of the
Clock
methods. We are using Java 9 to run our examples.
Contents
systemDefaultZone
Find thesystemDefaultZone
method signature.
public static Clock systemDefaultZone()
systemDefaultZone
is a static method that gives Clock
to return current instant using default time-zone. We can use it as following.
Clock clock = Clock.systemDefaultZone();
ZoneId.systemDefault()
with Clock.system
as following.
clock = Clock.system(ZoneId.systemDefault());
millis
Find themillis
method signature.
public long millis()
millis
returns the current millisecond instant of the clock measured from 1970-01-01T00:00Z (UTC). We should use millis
for high performance use cases. The value of millis
is equivalent to System.currentTimeMillis()
. Find the code snippet to use it.
package com.concretepage; import java.time.Clock; public class MillisExample { public static void main(String[] args) { Clock clock = Clock.systemDefaultZone(); System.out.println(clock.millis()); } }
1515492870941
instant
instant()
method returns Instant
object. Instant
is an instantaneous point on time line. instant()
returns current Instant
defined by the clock. Find the usage.
package com.concretepage; import java.time.Clock; import java.time.Instant; public class InstantExample { public static void main(String[] args) { Clock clock = Clock.systemDefaultZone(); Instant instant = clock.instant(); System.out.println(instant); } }
2018-01-10T04:31:19.607298400Z
offset
Find theoffset
method signature.
public static Clock offset(Clock baseClock, Duration offsetDuration)
offset
static method gives a clock that returns instant from the specified base clock with specified duration added. If the duration is negative, then obtained clock instant will be earlier than the given base clock. Using offset
we can obtain past and future instant of the given base clock. If we pass zero duration then we will get the same clock as given base clock. Find the usage.
package com.concretepage; import java.time.Clock; import java.time.Duration; public class OffsetExample { public static void main(String[] args) { Clock baseClock = Clock.systemDefaultZone(); //Obtained clock will be later than baseClock Clock clock = Clock.offset(baseClock, Duration.ofHours(120)); System.out.println(clock.instant()); //Obtained clock will be earlier than baseClock clock = Clock.offset(baseClock, Duration.ofHours(-120)); System.out.println(clock.instant()); //Obtained clock will be same as baseClock clock = Clock.offset(baseClock, Duration.ZERO); System.out.println(clock.instant()); } }
2018-01-14T15:23:14.951866600Z 2018-01-04T15:23:14.968867500Z 2018-01-09T15:23:14.968867500Z
system
Find thesystem
method signature.
public static Clock system(ZoneId zone)
system
is a static method that returns Clock
object for the given ZoneId
. Now this clock object will give current instant using specified time-zone. Find the system
method example for zone id America/Cuiaba.
package com.concretepage; import java.time.Clock; import java.time.ZoneId; public class SystemExample { public static void main(String[] args) { Clock clock = Clock.system(ZoneId.of("America/Cuiaba")); System.out.println(clock.instant()); } }
2018-01-09T17:06:39.831345700Z
systemUTC
systemUTC()
returns the Clock
that gives current instant using UTC time-zone.
package com.concretepage; import java.time.Clock; public class SystemUTCExample { public static void main(String[] args) { Clock clock = Clock.systemUTC(); System.out.println(clock.instant()); } }
2018-01-10T04:32:02.546754400Z
tick
Find thetick
method signature.
public static Clock tick(Clock baseClock, Duration tickDuration)
tick
returns clock using specified base clock. This clock will give instants nearest to occurrence of specified duration. Clock duration must be positive. Find the sample example.
package com.concretepage; import java.time.Clock; import java.time.Duration; public class TickExample { public static void main(String[] args) { Clock baseClock = Clock.systemDefaultZone(); Clock clock = Clock.tick(baseClock, Duration.ofMillis(3)); for(int i= 0; i < 5; i++) { System.out.println(clock.instant()); try { Thread.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } } } }
Output
2018-01-10T10:08:30.279Z 2018-01-10T10:08:30.297Z 2018-01-10T10:08:30.297Z 2018-01-10T10:08:30.300Z 2018-01-10T10:08:30.303Z
tickMillis
Find thetickMillis
method signature.
public static Clock tickMillis(ZoneId zone)
tickMillis
can be achieved by tick
method, too. Find the code snippet using tickMillis
.
Clock clock = Clock.tickMillis(zoneId);
tick
as following.
Clock clock = Clock.tick(Clock.system(zoneId), Duration.ofMillis(1));
tickMillis
.
package com.concretepage; import java.time.Clock; import java.time.ZoneId; public class TickMillisExample { public static void main(String[] args) { ZoneId zoneId = ZoneId.of("Asia/Calcutta"); Clock clock = Clock.tickMillis(zoneId); for(int i= 0; i < 5; i++) { System.out.println(clock.instant()); try { Thread.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } } } }
2018-01-10T09:54:22.788Z 2018-01-10T09:54:22.820Z 2018-01-10T09:54:22.835Z 2018-01-10T09:54:22.837Z 2018-01-10T09:54:22.839Z
tickSeconds
Find thetickSeconds
method signature.
public static Clock tickSeconds(ZoneId zone)
tickSeconds
gives a clock for the given time-zone. This clock will return current instant ticking in whole seconds. The nano-of-second field will be set to zero. The functionality of tickSeconds
can be achieved by tick
method, too. Find the code snippet using tickSeconds
.
Clock clock = Clock.tickSeconds(zoneId);
tick
as following.
Clock clock = Clock.tick(Clock.system(zoneId), Duration.ofSeconds(1));
tickSeconds
.
package com.concretepage; import java.time.Clock; import java.time.ZoneId; public class TickMillisExample { public static void main(String[] args) { ZoneId zoneId = ZoneId.of("Asia/Calcutta"); Clock clock = Clock.tickSeconds(zoneId); for(int i= 0; i < 5; i++) { System.out.println(clock.instant()); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
2018-01-10T10:24:51Z 2018-01-10T10:24:53Z 2018-01-10T10:24:55Z 2018-01-10T10:24:57Z 2018-01-10T10:24:59Z
tickMinutes
Find thetickMinutes
method signature.
public static Clock tickMinutes(ZoneId zone)
tickMinutes
gives a clock for the specified time-zone. This clock will return instant in whole minutes. Nano-of-second and second-of-minute fields will be set to zero. We will get all ticks in whole minutes. The functionality of tickMinutes
can be achieved by tick
method, too. Find the code snippet using tickMinutes
.
Clock clock = Clock.tickMinutes(zoneId);
tick
as following.
Clock clock = Clock.tick(Clock.system(zoneId), Duration.ofMinutes(1));
tickMinutes
.
package com.concretepage; import java.time.Clock; import java.time.ZoneId; public class TickMinutesExample { public static void main(String[] args) { ZoneId zoneId = ZoneId.of("Asia/Calcutta"); Clock clock = Clock.tickMinutes(zoneId); for(int i= 0; i < 3; i++) { System.out.println(clock.instant()); try { Thread.sleep(90000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
2018-01-10T13:26:00Z 2018-01-10T13:27:00Z 2018-01-10T13:29:00Z
fixed
Find thefixed
method signature.
public static Clock fixed(Instant fixedInstant, ZoneId zone)
fixed
method always returns the same instant as specified. It is useful in testing. ZoneId
gives the time-zone that is used to convert the instant to date-time. Find the sample example to use fixed
method.
package com.concretepage; import java.time.Clock; import java.time.Instant; import java.time.ZoneId; public class FixedExample { public static void main(String[] args) { Instant instant = Instant.parse("2018-01-08T15:34:42.00Z"); ZoneId zoneId = ZoneId.of("Asia/Calcutta"); Clock clock = Clock.fixed(instant, zoneId); System.out.println(clock.instant()); } }
2018-01-08T15:34:42Z
withZone
withZone(ZoneId zone)
returns a copy of the clock with a different time-zone. If we have a clock instance for a time-zone, we can make a copy of that clock for different time-zone using withZone
method. Find the sample example.
package com.concretepage; import java.time.Clock; import java.time.ZoneId; public class WithZoneExample { public static void main(String[] args) { ZoneId zone1 = ZoneId.of("Asia/Aden"); Clock clock1 = Clock.system(zone1); System.out.println(clock1.instant()); ZoneId zone2 = ZoneId.of("America/Cuiaba"); Clock clock2 = clock1.withZone(zone2); System.out.println(clock2.instant()); } }
2018-01-10T13:59:06.657249300Z 2018-01-10T13:59:06.704049300Z
getZone
getZone()
gives the time-zone used to create date and time. Find the sample example.
package com.concretepage; import java.time.Clock; import java.time.ZoneId; public class GetZoneExample { public static void main(String[] args) { Clock clock = Clock.systemDefaultZone(); ZoneId zone = clock.getZone(); System.out.println(zone.getId()); } }
Asia/Calcutta
equals
equals
method checks if two clocks are equal. Find the sample example.
package com.concretepage; import java.time.Clock; public class EqualsExample { public static void main(String[] args) { Clock clock = Clock.systemDefaultZone(); System.out.println(clock.equals(Clock.systemDefaultZone())); System.out.println(clock.equals(Clock.systemUTC())); } }
true false