The Wayback Machine - https://web.archive.org/web/20240623131240/https://www.geeksforgeeks.org/java-time-clock-class-in-java/
Open In App

java.time.Clock Class in Java

Last Updated : 09 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Java Clock class is present in java.time package. It was introduced in Java 8 and provides access to current instant, date, and time using a time zone.

The use of the Clock class is not mandatory because all date-time classes also have a now() method that uses the system clock in the default time zone. The main purpose of using Clock class is to plug an alternate clock wherever required.  Applications use an object to obtain the current time rather than a static method. This makes the testing easy. We can pass a Clock as an argument to a method that requires a current instant.

Declaration:

public abstract class Clock extends Object

It is an abstract class, so we can’t instantiate it, But we can use several static methods to access its instance.

systemUTC() Method:

public static Clock systemUTC()

This method returns a Clock instance of the current instant in the UTC time zone. This is best when you need a current instant without a date or time.

Java




// Java program for creating instance of Clock
import java.time.Clock;
 
public class GFG {
     
    // main method
    public static void main(String[] args) {
         
        // creating a Clock instance using
        // systemUTC() method of Clock class
        Clock clock = Clock.systemUTC();
         
        // getting the current instant defined by clock
        System.out.println("UTC time = " + clock.instant());
    }
}


Output

UTC time = 2021-02-07T16:16:43.863267Z

systemDefaultZone() Method

public static Clock systemDefaultZone()

This method returns a Clock instance of the current instant using the default time zone of the system.

Java




// Java program for creating instance of Clock
 
import java.time.Clock;
 
public class GFG {
 
    // main method
    public static void main(String[] args)
    {
 
        // creating a Clock instance using
        // systemDefaultZone() method of Clock class
        Clock clock = Clock.systemDefaultZone();
 
        // it will print "SystemClock[Asia/Calcutta]" for me.
        // The output may be different because of server
        // system clock.
        System.out.println(clock);
 
        // printing zone of clock instance
        // it will print "Time zone : Asia/Calcutta" for me.
        System.out.println("Time Zone : "
                           + clock.getZone());
    }
}


 
 

Output

SystemClock[Etc/UTC]
Time Zone : Etc/UTC

Methods

Method Description
fixed(Instant fixedInstant, ZoneId zone) Used to get a Clock which always returns the same instant.
getZone() Returns the time zone of the given clock.
instant() Used to get current instant of the clock.
millis() Returns current millisecond instant of the clock. 
offset(Clock baseClock, Duration offsetDuration) Returns a Clock that returns instant from the specified clock with the specified duration added. 
system(ZoneId zone) Returns a Clock object of current instant for the specified zone id.
systemDefaultZone() Returns a Clock instance of the current instant using the default time zone of the system.
systemUTC()      Returns a Clock instance of current instant in UTC time zone.
tick(Clock baseClock, Duration tickDuration) Returns instants from the specified base clock truncated to the nearest occurrence of the specified duration.
tickMinutes(ZoneId zone) Returns a Clock object of current instant ticking in whole minutes for the given time zone.
tickSeconds(Zoneid zone) Returns a Clock object of current instant ticking in whole seconds for the given time zone.
withZone(ZoneId zone) Create a copy of the clock with the different time zone.

 



Previous Article
Next Article

Similar Reads

Android | How to display Analog clock and Digital clock
Pre-requisites: Android App Development Fundamentals for Beginners Guide to Install and Set up Android Studio Android | Starting with first app/android project Android | Running your first Android app Analog and digital clocks are used for display the time in android application. Analog clock: Analog clock is a subclass of View class. It represents
2 min read
Java Clock tickMinutes() method in Java with Examples
java.time.Clock.tickMinutes(ZoneId zone) method is a static method of Clock class that returns the current instant ticking in whole minutes using the best available system clock and the time-zone of that instant is same as the time-zone passed as a parameter. Nanosecond and second fields of the clock are set to zero to round the instant in the whol
3 min read
Java Clock withZone() method in Java with Examples
The java.time.Clock.withZone(ZoneId zone) method is a method of Clock class which returns a clock copy of clock object on which this method is applied, with a different time-zone. If there is a clock and it is required to change the zone of clock but not other properties, then withZone() method is used. This method takes zone as parameter which is
3 min read
Java 8 Clock instant() method with Examples
Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8. instant() method of Clock class returns a current instant of Clock object as Instant Class Object. Instant generates a timestamp to represent machine time. So this method generates a timestamp for clock object. Here returned In
3 min read
Java 8 Clock hashCode() method with Examples
Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8. hashCode() method of Clock class returns the hash code for this Clock Object. Clocks object overrides this method based on their state. If clock object is not overridden, the behavior of this method is defined by Object.hashCod
2 min read
Java 8 Clock getZone() method with Examples
Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8.getZone() method of Clock class returns the time-zone used to create dates and times of Clock class. Every Clock class needs a Time Zone for obtaining the current instant of time and convert that instance to a date or time. So t
2 min read
Java 8 Clock fixed() method with Examples
Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8. fixed() method of Clock class returns a clock object and the Clock object returns the same instant. Clock object is returned by calling Clock.fixed(parameters) simply returns the same instant as specified using parameters. The
2 min read
Java 8 Clock millis() Method with Examples
Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8.The millis() method of Clock class returns the current instant of the clock in milliseconds. A millisecond instant is measured from 1970-01-01T00:00Z (UTC) to the current time. This method does the same work as System.currentTim
2 min read
Java 8 Clock equals() Method with Examples
Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8.The equals() method of java.time.Clock class checks if two Clock objects are equal or not. If clocks are equal then it returns true, else it returns false. This equals method of clock class overrides this Object.equals(java.lang
3 min read
Java 8 Clock offset() method with Examples
Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8. The offset() method is a static method of Clock class which returns a clock with instant equal to the sum of the instants of clock passed as parameter and specific Offset duration. If the duration added is positive, then the re
3 min read
Practice Tags :