Javatpoint Logo
Javatpoint Logo

Java Clock class

Java Clock class is used to provide an access to the current, date and time using a time zone. It inherits the Object class.

Because all date-time classes contain a now() function that uses the system clock in the default time zone, using the Clock class is not required. The aim of the Clock class is to allow you to plug in another clock whenever you need it. Instead of using a static method, applications utilise an object to get the current time. It simplifies the testing process. A method that requires a current instant can take a Clock as a parameter.

Java Clock Class Declaration

Let's see the declaration of java.time.Clock class.

Methods of Java Clock Class

Method Description
abstract ZoneId getZone() It is used to get the time-zone being used to create dates and times.
abstract Instant instant() It is used to get the current instant of the clock.
static Clock offset(Clock baseClock, Duration offsetDuration) It is used to obtain a clock that returns instants from the specified clock with the specified duration added
static Clock systemDefaultZone() It is used to obtain a clock that returns the current instant using the best available system clock, converting to date and time using the default time-zone.
static Clock systemUTC() It is used to obtain a clock that returns the current instant using the best available system clock, converting to date and time using the UTC time zone.
boolean equals(Object obj) It checks if this clock is equal to another clock.
static Clock fixed(Instant fixedInstant, ZoneId zone) It obtains a clock that always returns the same instant.
static Clock system(ZoneId zone) It obtains a clock that returns the current instant using best available system clock.
int hashCode() It gets the time-zone being used to create dates and times.
long millis() It gets the current millisecond instant of the clock.
static Clock tick(Clock baseClock, Duration tickDuration) It obtains a clock that returns instants 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 best available system clock.
static Clock tickSeconds(ZoneId zone) It obtains a clock that returns the current instant ticking in whole seconds using best available system clock.
static Clock withZone(ZoneId zone) It returns a copy of this clock with a different time-zone.

Java Clock class Example: getZone()

ClockExample1.java

Test it Now

Output:

Asia/Calcutta

Java Clock class Example: instant()

ClockExample2.java

Test it Now

Output:

2017-01-14T07:11:07.748Z

Java Clock class Example: systemUTC()

ClockExample3.java

Test it Now

Output:

2017-01-14T07:11:07.748Z

Java Clock class Example: offset()

ClockExample4.java

Test it Now

Output:

2017-01-14T14:15:25.389Z

Next TopicJava ZonedDateTime





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA