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

java.time.Clock Class in Java

Last Updated : 09 Mar, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
Clock tick() Method in Java with Examples
tick(Clock baseClock, Duration tickDuration) method of java.time.Clock is a static method of Clock class that returns a clock that returns instants from the base clock rounded to the nearest occurrence of the specified duration in the parameter. The specified base clock duration must be positive, neither negative nor null. The clock will tick as pe
4 min read
Clock tickSeconds() method in Java with Examples
java.time.Clock.tickSeconds(ZoneId zone) method is a static method of Clock class that returns a clock that returns the current instant ticking in whole Seconds using the best available system clock and zone of instant is same as the instant passed as a parameter. The returned clock is also immutable, thread-safe, and Serializable and this method i
2 min read
Clock systemDefaultZone() Method in Java with Examples
java.time.Clock.systemDefaultZone() method is a static method of Clock class which returns a clock that returns the current instant of the clock using best available system clock where Zone of the returned clock is default time-zone. This method can use System.currentTimeMillis(), or other higher resolution clock for its implementation if the clock
2 min read
Clock systemUTC() Method in Java with Examples
java.time.Clock.systemUTC() method is a static method of Clock class which returns a clock that returns the current instant of the clock using the best available system clock where Zone of the returned clock is UTC time-zone. When the current instant is needed without the date or time, then use systemUTC() method in place of systemDefaultZone(). Wh
2 min read
Clock system() Method in Java with Examples
java.time.Clock.system(ZoneId zone) method is a static method of Clock class which returns a clock that returns the current instant of the clock using best available system clock with ZoneID of the returned clock is set to the ZoneID passed. This method can use System.currentTimeMillis(), or other higher resolution clock if the clock is available t
3 min read
Java Applet | How to display a Digital Clock
In this article, we shall be animating the applet window with a 1-second delay. The idea is to display the system time of every instance. Approach: Here 6 seven-segment displays are created using the Java Applet library to print the system time in HH:MM:SS format. Each segment of the seven-segment display, numbered as follows can be lit in differen
3 min read
Java Applet | How to display an Analog Clock
In this article, we shall be animating the applet window to show an Analog Clock with a 1-second delay. The idea is to display the system time of every instance. Approach: Each hand of the clock will be animating with 1-second delay keeping one end at the centre. The position of the other end can be derived by the system time. The angle formed by a
3 min read
HijrahChronology dateNow(Clock) method in Java with Example
The dateNow() method of java.time.chrono.HijrahChronology class is used get the current local date according to Hijrah calendar system from the specified clock object. Syntax: public HijrahDate dateNow(Clock clock) Parameter: This method takes the object of clock as a parameter. Return Value: This method returns the local date according to Hijrah c
2 min read
How to draw a Clock using StillClock in Java
StillClock class is used to displays an analog clock and uses the MessagePanel to display the hour, minute, and second in a panel. We use javax.swing.JPanel to display the StillClock. This article helps to develop a class that displays a clock on a panel. Steps used to draw the clock: To draw a clock, draw a circle and three hands for a second, min
3 min read
HijrahDate now(Clock) method in Java with Example
The now() method of java.time.chrono.HijrahDate class is used to get the current hijrah date according to the hijrah calendar system from the specified clock. Syntax: public static HijrahDate now(Clock clock) Parameter: This method takes the object of the clock on the basis of which Hijrah date is going to be formed. Return Value: This method retur
2 min read
IsoChronology dateNow(Clock) method in Java with Example
The dateNow() method of java.time.chrono.IsoChronology class is used get the current local date according to Iso calendar system from the specified clock object. Syntax: public LocalDate dateNow(Clock clock) Parameter: This method takes the object of clock as a parameter. Return Value: This method returns the LocalDate according to Hijrah calendar
2 min read
JapaneseChronology dateNow(Clock) method in Java with Example
The dateNow() method of java.time.chrono.JapaneseChronology class is used get the current Japanese date according to Japanese calendar system from the specified clock object. Syntax: public JapaneseDate dateNow(Clock clock) Parameter: This method takes the object of clock as a parameter which is used get the current Japanese date according to Japan
2 min read
JapaneseDate now(Clock) method in Java with Examples
The now() method of java.time.chrono.JapaneseDate class is used to get the current Japanese date according to the Japanese calendar system from the specified clock. Syntax: public static JapaneseDate now(Clock clock) Parameter: This method takes the object of the clock on the basis of which Japanese date is going to be formed. Return Value: This me
2 min read
MinguoChronology dateNow(Clock) method in Java with Example
The dateNow() method of java.time.chrono.MinguoChronology class is used get the current Minguo date according to Minguo calendar system from the specified clock object. Syntax: public MinguoDate dateNow(Clock clock) Parameter: This method takes the object of clock as a parameter which is used get the current Minguo date according to Minguo calendar
2 min read
YearMonth now(clock) method in Java with Examples
now(Clock clock) method of the YearMonth class in Java is used to obtain the current year-month from the specified clock. Syntax: public static YearMonth now(Clock clock) Parameters: This method accepts clock as parameter which represents the clock to use. Return value: This method returns the current year-month. Below programs illustrate the now(C
1 min read
LocalTime now(clock) method in Java with Examples
The now(Clock clock) method of the LocalTime class in Java is used to get the current time from the specified clock. Syntax: public static LocalTime now( Clock clock) Parameters: This method accepts clock as parameter which represents the clock to use. Return value: This method returns the current time. Below programs illustrate the now(Clock clock
1 min read
MonthDay now(Clock) method in Java with Examples
The now(Clock clock) method of the MonthDay class in Java is used to get the current month-day from the specified clock. Syntax: public static MonthDay now(Clock clock) Parameters: This method accepts clock as parameter which represents the clock to use. Return value: This method returns the current month-day. Below programs illustrate the now(Cloc
1 min read
ThaiBuddhistChronology dateNow(Clock) method in Java with Example
The dateNow() method of java.time.chrono.ThaiBuddhistChronology class is used get the current ThaiBuddhist date according to ThaiBuddhist calendar system from the specified clock object. Syntax: public ThaiBuddhistDate dateNow(Clock clock) Parameter: This method takes the object of clock as a parameter which is used get the current ThaiBuddhist dat
2 min read
ThaiBuddhistDate now(Clock) method in Java with Example
The now() method of java.time.chrono.ThaiBuddhistDate class is used to get the current ThaiBuddhist date according to the ThaiBuddhist calendar system from the specified clock. Syntax: public static ThaiBuddhistDate now(Clock clock) Parameter: This method takes the object of the clock on the basis of which ThaiBuddhist date is going to be formed. R
2 min read
Why BufferedReader class takes less time for I/O operation than Scanner class in java
In Java, both BufferReader and Scanner classes can be used for the reading inputs, but they differ in the performance due to their underlying implementations. The BufferReader class can be generally faster than the Scanner class because of the way it handles the input and parsing. This article will guide these difference, provide the detailed expla
3 min read
Practice Tags :