What is Clock in Java?

The Clock class in Java is a part of the java.time package introduced in Java 8. It is an abstraction that represents a time source, which can be used to obtain the current time or date, like a wall clock or a more abstract time provider.

Clock can also be used to manage and manipulate time further. For example, you can use it to get the current time in milliseconds or to perform operations based on a specific time zone or a fixed offset.

Here is a simple example of how to use the Clock class:


Clock clock = Clock.systemDefaultZone();
ZonedDateTime zonedDateTime = ZonedDateTime.now(clock);
System.out.println("Current date and time: " + zonedDateTime);
    

Clock Java java.time current time date management