The Clock class in Java belongs to the java.time package and was introduced in Java 8. It provides access to the current instant and a time zone, and it is used by date-time classes to obtain the current date and time. The Clock class also supports alternate clocks, which makes time-dependent code easier to test.
- Clock is an abstract class, so it is not instantiated directly.
- Static methods such as systemUTC() and systemDefaultZone() return Clock instances.
- A Clock object provides methods such as instant(), millis(), getZone(), and withZone() to work with the current time and time zone.
Example: Program creates a Clock using systemUTC() and displays the current UTC time.
import java.time.Clock;
public class GFG {
public static void main(String[] args) {
Clock clock = Clock.systemUTC();
System.out.println("Current UTC time: " + clock.instant());
}
}
Output
Current UTC time: 2026-09-08T04:58:07.671346520Z
Explanation: The program creates a UTC-based Clock using systemUTC() and uses instant() to display the current UTC instant.
Syntax
public static Clock systemUTC()
Ways To Create and Use Clock in Java
The Clock class provides static methods for obtaining different clock implementations. The following examples demonstrate UTC time and the system's default time zone.
1. Using systemUTC() Method
The systemUTC() method returns a Clock representing the current instant in the UTC time zone. The instant() method retrieves the current instant from this clock.
import java.time.Clock;
public class GFG {
public static void main(String[] args) {
Clock clock = Clock.systemUTC();
System.out.println("UTC time = " + clock.instant());
}
}
Output
UTC time = 2026-09-08T05:00:05.644837854Z
Explanation: The program creates a Clock object using systemUTC(). The instant() method then displays the current instant in UTC. The exact output varies according to the system clock.
2. Using systemDefaultZone() Method
The systemDefaultZone() method returns a Clock based on the system's default time zone. The getZone() method returns the time zone associated with the clock.
import java.time.Clock;
public class GFG {
public static void main(String[] args) {
Clock clock = Clock.systemDefaultZone();
System.out.println(clock);
System.out.println("Time Zone: " + clock.getZone());
}
}
Output
SystemClock[UTC] Time Zone: UTC
Explanation: The program creates a clock using the system's default time zone. It then prints the clock and retrieves its time zone using getZone(). The output depends on the default time zone configured on the system.
Commonly Used Methods of Clock
| Method | Description |
|---|---|
fixed(Instant, ZoneId) | Returns a clock that always returns the same instant. |
getZone() | Returns the time zone of the clock. |
instant() | Returns the current instant. |
millis() | Returns the current time in milliseconds. |
offset(Clock, Duration) | Returns a clock with the specified duration added to the base clock. |
system(ZoneId) | Returns a clock for the specified time zone. |
systemDefaultZone() | Returns a clock using the system's default time zone. |
systemUTC() | Returns a clock using the UTC time zone. |
tick(Clock, Duration) | Returns a clock that ticks at the specified duration. |
tickMinutes(ZoneId) | Returns a clock that ticks in whole minutes for the specified time zone. |
tickSeconds(ZoneId) | Returns a clock that ticks in whole seconds for the specified time zone. |
withZone(ZoneId) | Returns a copy of the clock with a different time zone. |
Note: Clock is useful for testing time-dependent code because an application can use a fixed or custom clock instead of relying directly on the system clock.