Java Clock class gives you access to obtain current date-time using a time-zone. Use of Clock class is not mandatory as all date-time classes have now() method which is used to get current date-time information from system in default time-zone. The main purpose of Clock class is to allow access to an alternate clock which can be plugged in an application whenever needed.
Java Clock class:
public abstract class Clock extends Object
Java Clock class – Method Summary
Method | Description |
---|---|
boolean equals(Object obj) | Checks if this clock is equal to the other specified clock |
abstract ZoneId getZone() | Gets the time-zone. |
int hashCode() | Returns hash code for this code. |
static Clock fixed(Instant fixedInstant, ZoneId zone) | It obtains a clock that always returns the same instant. |
long millis() | It returns the current millisecond instant of the clock. |
static Clock withZone(ZoneId zone) | It returns a copy of this clock with the specified time-zone. |
static Clock systemUTC() | Returns a copy of this clock after converting the date-time in UTC timezone. |
static Clock offset(Clock baseClock, Duration offsetDuration) | It is used to obtain a clock using specified clock with the specified duration added. |
static Clock system(ZoneId zone) | Returns a clock that returns the current instant using the system clock. |
static Clock tick(Clock baseClock, Duration tickDuration) | It obtains a clock that returns the current instant from the specified clock truncated to the nearest occurrence of the specified duration. |
static Clock tickMinutes(ZoneId zone) | It obtains a clock that returns the current instant ticking in whole minutes using the system clock. |
static Clock tickSeconds(ZoneId zone) | It obtains a clock that returns the current instant ticking in whole seconds using the system clock. |
Java Clock class: getZone(), instant() & millis() methods example
Method getZone()
is used to obtain the system default time-zone. The method instant()
is used to obtain the current instant and method millis()
returns the current milliseconds instant of the clock.
import java.time.Clock; public class JavaExample { public static void main(String[] args) { Clock clock = Clock.systemDefaultZone(); System.out.println(clock.getZone()); System.out.println(clock.instant()); System.out.println(clock.millis()); } }
Output:
Asia/Kolkata 2022-06-12T13:13:28.420230Z 1655039608440
Java Clock class: systemUTC() method example
This method returns the clock in the UTC timezone. It obtains the date-time from system clock and converts it into UTC timezone.
import java.time.Clock; public class JavaExample { public static void main(String[] args) { Clock clock = Clock.systemUTC(); System.out.println(clock.instant()); } }
Output:
2022-06-12T13:15:59.036142Z
Java Clock class: offset() method example
The offset()
method returns a clock that returns the current instant using specified clock with the specified duration added. Here we have obtained the system default clock using systemDefaultZone()
method and then we supplied this clock and duration to the offset method to display the current system instant after adding the specified duration.
import java.time.Clock; import java.time.Duration; public class JavaExample { public static void main(String[] args) { //getting default system clock Clock clock = Clock.systemDefaultZone(); System.out.println("Timezone: "+clock); System.out.println("Current Instant: "+clock.instant()); //duration set to 3 days Duration duration = Duration.ofDays(3); Clock clock2 = Clock.offset(clock, duration); System.out.println(clock2.instant()); } }
Output:
Timezone: SystemClock[Asia/Kolkata] Current Instant: 2022-06-12T13:27:59.817263Z 2022-06-15T13:27:59.837646Z
Leave a Reply