Introduction to Java Clock
This article will learn and understand the need and use of the Java Programming Language’s Clock Class. Java class for Clock is part of Date Time API. Java Date Time API falls under java.time package. In Java 8, this Clock Class was added with functionality to be a better time provider for an instant. Local and Zoned are the major classification under the mentioned API. Simple Data Time API with no complications of other time zone handling is Local, and Zoned is a specific date-time API focusing on a variety of time zones.
This clock class provides access to the current instant, date and time with the help of time-zone. It cannot instantiate as it is an abstract class but provides several static methods that can be. Now to look on the brighter side, this clock class can be implemented rather than using two other methods, TimeZone.getDefault() and currentTimeMillis().
FixedClock, OffsetClock, SystemClock, and TickClock are the four implementations of Java Clock; these are static methods and part of the Java class. The best Practice for better implementation of the Clock class is to pass it into a method where the current instant is required. Not to overlook that the use of the clock is optional. So, now that we have understood the Clock class in java let us now understand the syntax and its usage.
Syntax:
public abstract class Clock extends Object
This is a simple standard syntax for the Clock class, which, as we can see, extends Object. Proper implementation of the above declaration will result in the expected output for the Clock class. Now that we have understood the definition and syntax for Clock class let us learn its methods.
Methods of Java Clock
Just like any other class in Java Programming Language, Clock comes with various methods for respective usages. The following are the methods with respective descriptions.
1. instant
Simply return the current instant of the clock and not null.
public abstract Instant instant()
2. equals
Compares if the clock is equal to any other clock. Returns true if the clocks are equal, else false. This method of clock class overrides the Object.equals method.
public boolean equals(Object obj)
3. getZone
To get the time zone currently used never returns null. It obtains the current instant and converts it to a time or date with time zone use.
public abstract ZoneId getZone()
4. systemdefaultone
Using the best available system clock, this method returns the current instant, then converting it to date and time with the default time zone. May use System.currentTimeMillis() and the return is immutable and thread-safe.
public static Clock systemDefaultZone()
5. systemUTC
Similar to systemDefaultZone, best to use when the date or time is not required. Rest everything is the same, from using the best available system clock to return being immutable.
public static Clock systemUTC()
Examples to Implement Java Clock
Other than the above-mentioned methods, the Clock class provides other methods for specific use. To demonstrate the above-mentioned methods, we will implement these methods of Clock class in examples. Refer to the following programs, which include these methods and their output.
Example #1
Code:
import java.time.Clock;
import java.time.ZoneId;
public class java_clock {
public static void main(String[] args) {
Clock j_clock = Clock.systemDefaultZone();
System.out.println(j_clock.getZone());
System.out.println(j_clock.instant());
System.out.println(j_clock.systemDefaultZone());
}
}
Output:
Code Interpretation: This example demonstrates the implementation of the getZone, instant and systemDefaultZone methods. Begin with two java packages import and then create our class java_clock. Then we initialized our main class with a simple Clock object. Finally, we have out three output print lines with respective calls for methods. The first method prints the Time Zone for the current system clock; here, it prints “Asia/Kolkata”. The second method simply returns the clock’s current instant, i.e. the Date and Time; for me, it prints the current Date and Time. Then the third method is similar to the first one, where it prints the system clock’s time zone.
So with the above code example, we have demonstrated the implementation of three basic methods of Clock class in Java. Two for Zone and an Instant method.
Example #2
Code:
import java.time.Clock;
import java.time.ZoneId;
public class java_clock_equals {
public static void main(String[] args) {
Clock java_clock1 = Clock.system(ZoneId.of("Etc/UTC"));
System.out.println(java_clock1.toString());
Clock java_clock2 = Clock.systemDefaultZone();
System.out.println(java_clock2.toString());
boolean equalResponse = java_clock1.equals(java_clock2);
System.out.println("Are the both clocks equal:" + equalResponse);
}
}
Output:
Code Interpretation: In the above code, we have implemented the equals method of the clock class. Similar to the first program, we began with importing two required Java Packages. Then created our class java_clock_equals and our main class within. Later we’ve created an object for our clock with an assigned Zone ID of “Etc/UTC” and printed the same for confirmation. Then another object for Clock with the system’s default Zone and a print statement for the same. Also, check that we have used the toString method to make sure the output is printed without any error, though removing toString won’t make any difference. In the second last statement, we have declared a boolean variable equal response, and this is where the comparison of equals happens. We have passed the values of both the clocks with the equals method. java_clock1.equals(java_clock2); this is where the value of clock1 and clock2 are compared, and then the output returns as in boolean value. In the last line, we have our output print statement with the value received after the comparison. Here the output will always be either true or false as it is boolean.
We can see that the output is in three sentences, with the first sentence is the assigned Time Zone, then the system’s current Time Zone and finally the comparison and its output.
Conclusion
To conclude, we have understood that the Java Clock class was introduced in Java 8. It provides instant if the current system’s Time and Date. We understood the methods along with the proper syntax and a clear explanation. We worked out two examples with code to demonstrate these methods, one for two methods related to Zone and Instant methods and another for the equals method. Finally, with code, its explanation, and the respective screenshot, we understood the Java Clock class’s working.
Recommended Articles
This is a guide to Java Clock. Here we discuss the basic concept and methods of the java clock along with the examples and code implementation. You may also look at the following articles to learn more –