Java Clock class and its Methods with Examples [2021]
Java Clock class is one of the java.time package classes introduced in Java 8 which provides access to current instant, date-time using a timezone.
A Clock is a good alternative to old System.currentTimeMillis() and TimeZone.getDefault() methods.
Here is the declaration of Java Clock class
public abstract class Clock extends Object
The clock is an abstract class it has 4 implementing classes which are shown in below image
These four classes FIxedClock, OffsetClock, SystemClock, TickClock implements Clock abstract class.
Here is the list of methods that we are going to cover in this tutorial.
- public abstract Instant instant()
- public long millis()
- public abstract ZoneId getZone()
- public static Clock systemDefaultZone()
- public static Clock systemUTC()
- public static Clock system(ZoneId zone)
- public abstract Clock withZone(ZoneId zone)
- public static Clock offset(Clock baseClock,Duration offsetDuration)
- public static Clock fixed(Instant fixedInstant,ZoneId zone)
- public static Clock tick(Clock baseClock, Duration tickDuration)
- public static Clock tickSeconds(ZoneId zone)
- public static Clock tickMinutes(ZoneId zone)
Java Clock Class Examples
1. Java Clock Example: public abstract Instant instant()
This method returns the current instant of the clock selected.
Clock clock = Clock.systemDefaultZone();
System.out.println(clock.instant());
Output:
2020-07-19T16:23:37.196Z
2. Java Clock Example: public long millis()
This method returns the current millisecond instant of the clock.
Clock clock = Clock.systemDefaultZone();
// Give milliseconds of current instant.
System.out.println(clock.millis());
Output:
1595175817228
3. Java Clock Example: public abstract ZoneId getZone()
This method returns the time-zone that is being used to create dates and times.
Clock clock = Clock.systemDefaultZone();
// Gives the zone of clock
System.out.println(clock.getZone());
Output:
Asia/Calcutta
4. Java Clock Example: public static Clock systemDefaultZone()
This method gets a clock that returns the current instant object using the best available system clock, converting to date and time using the default time-zone.
// Create instance of SystemClock
Clock clock1 = Clock.systemDefaultZone();
System.out.println(clock1 + "--" + clock1.instant());
Output:
SystemClock[Asia/Calcutta]--2020-07-19T16:23:37.216Z
5. Java Clock Example: public static Clock systemUTC()
This method returns a clock that returns the current instant object using the best available system clock, converting to date and time using the UTC time-zone.
// Create instance of SystemClock
Clock clock3 = Clock.systemUTC();
System.out.println(clock3 + "--" + clock3.instant());
Output:
SystemClock[Z]--2020-07-19T16:23:37.224Z
6. Java Clock Example: public static Clock system(ZoneId zone)
This method returns a clock that returns the current instant using the best available system clock. Conversion from instant to date or time uses the specified time-zone.
// Create instance of SystemClock
Clock clock2 = Clock.system(ZoneId.of("US/Eastern"));
System.out.println(clock2 + "--" + clock2.instant());
Output:
SystemClock[US/Eastern]--2020-07-19T16:23:37.224Z
7. Java Clock Example: public static Clock offset(Clock baseClock,Duration offsetDuration)
This method returns a clock that returns instants from the specified clock with the specified duration added.
If the duration is negative, the instants will be earlier than the current date and time.
The main use case for this is to simulate running in the future or in the past.
A duration of zero would have no offsetting effect. Passing zero will return the underlying clock.
// Create instance of SystemClock
Clock clock3 = Clock.systemUTC();
System.out.println(clock3 + "--" + clock3.instant());
// Create instance of OffsetClock
Clock clock7 = Clock.offset(clock3, Duration.ofHours(5));
System.out.println(clock7 + "--" + clock7.instant());
Output:
SystemClock[Z]--2020-07-19T16:23:37.224Z
OffsetClock[SystemClock[Z],PT5H]--2020-07-19T21:23:37.228Z
8. Java Clock Example: public abstract Clock withZone(ZoneId zone)
This method returns a copy of this clock with a different time-zone.
This method returns a clock with similar properties but using a different time-zone.
In below example, we are getting clockCalcutta with a timezone of America/Guyana
ZoneId americaGuyana = ZoneId.of("America/Guyana");
Clock clockGuyana = Clock.system(americaGuyana);
System.out.println(americaGuyana + "-- " + clockGuyana.instant());
ZoneId zoneCalcutta = ZoneId.of("Asia/Calcutta");
Clock clockCalcutta = clockGuyana.withZone(zoneCalcutta);
System.out.println(clockCalcutta + "-- " + clockCalcutta.instant());
Output:
America/Guyana-- 2020-07-19T16:23:37.228Z
SystemClock[Asia/Calcutta]-- 2020-07-19T16:23:37.228Z
9. Java Clock Example: public static Clock fixed(Instant fixedInstant,ZoneId zone)
This java clock fixed method returns a clock that always returns the same instant.
The main use case for this is in testing, where the fixed clock ensures tests are not dependent on the current clock.
// Create instance of SystemClock
Clock clock1 = Clock.systemDefaultZone();
// Create instance of FixedClock
Clock clock8 = Clock.fixed(clock1.instant(), ZoneId.of("Asia/Calcutta"));
System.out.println(clock8 + "--" + clock8.instant());
Output:
FixedClock[2020-07-19T16:23:37.228Z,Asia/Calcutta]--2020-07-19T16:23:37.228Z
10. Java Clock Example: public static Clock tick(Clock baseClock, Duration tickDuration)
This method returns a clock that returns instants from the specified clock truncated to the nearest occurrence of the specified duration. This clock will only tick as per the specified duration.
Clock clock3 = Clock.systemUTC();
System.out.println(clock3 + "--" + clock3.instant());
// Create instance of TickClock
Clock clock4 = Clock.tick(clock3, Duration.ofHours(96));
System.out.println(clock4 + "--" + clock4.instant());
Output:
SystemClock[Z]--2020-07-19T16:23:37.224Z
TickClock[SystemClock[Z],PT96H]--2020-07-17T00:00:00Z
11. Java Clock Example: public static Clock tickSeconds(ZoneId zone)
This method returns a clock that returns the current instant ticking in whole seconds using the best available system clock.
This clock will always have the nano-of-second field set to zero.
This ensures that the visible time ticks in whole seconds.
This is similar to tick(system(zone), Duration.ofSeconds(1))
// Create instance of TickClock
Clock clock6 = Clock.tickSeconds(ZoneId.of("US/Eastern"));
System.out.println(clock6 + "--" + clock6.instant());
Output:
TickClock[SystemClock[US/Eastern],PT1S]--2020-07-19T16:23:37Z
12. Java Clock Example: public static Clock tickMinutes(ZoneId zone)
This method returns a clock that returns the current instant ticking in whole minutes using the best available system clock.
This clock will always have the nano-of-second and second-of-minute fields set to zero.
This ensures that the visible time ticks in whole minutes.
It is similar to tick(system(zone), Duration.ofMinutes(1))
// Create instance of TickClock
Clock clock5 = Clock.tickMinutes(ZoneId.of("US/Eastern"));
System.out.println(clock5 + "--" + clock5.instant());
Output:
TickClock[SystemClock[US/Eastern],PT1M]--2020-07-19T16:23:00Z
Conclusion:
In this tutorial, we have covered all the methods of java time clock class with examples. Clock class is the best alternative for java.util.Date class that we use till now.
Thanks for reading this tutorial so far. If you like this tutorial then please share it with your friends and colleagues. If you have any questions, doubts, suggestions, or feedback then please drop a comment and I'll try to answer your question.
Happy Learning!!!
More Examples you may like
Comments
Post a Comment
If you have any doubts, Please let me know.