Java introduced the new Date & Time API as java.time package with Java 8 by adding all that was missing while dealing with Date/Time/Time Zones, one of such classes is called the Clock.
⛏️ The Instances of Clock class can be used to get the current instant, date, and time using a time zone.
Example:Clock newYorkClock = Clock.system(ZoneId.of("America/New_York"));
You may not often use Clock class while dealing with date/time. When you create the objects of LocalDate, LocalDateTime, Instant, or ZonedDateTime, you would notice that the API makes use of the Clock class to get you the date and time.
Example: now method of LocalDatepublic static LocalDate now() {
return now(Clock.systemDefaultZone());
}
Example: now method of Instant
public static Instant now() {
return Clock.systemUTC().instant();
}
The Clock gets the date/time from the system clock in the default time zone.
Some methods of java.time.Clock class
1. public static Clock systemUTC()Example:
public static void main(String[] args) {
Clock clockUtc = Clock.systemUTC();
Instant instantUtc = clockUtc.instant();
System.out.println(instantUtc);
}
2022-05-14T11:21:22.192486Z
2. public static Clock systemDefaultZone()
Example:
This will get the Clock object based on the System Timezone which is America/New_York in this case,
public static void main(String[] args) {
Clock clockDefaultTimeZone = Clock.systemDefaultZone();
System.out.println(clockDefaultTimeZone.getZone());
}
America/New_York
3. public static Clock system(ZoneId zone)
Example:
This will get the Clock object based on the set Time ZoneID and using the best available system clock, it may use the System.currentTimeMillis() or any other higher resolution clock if one is available,
public static void main(String[] args) {
Clock systemClockTimeAmericaChicago = Clock.system(ZoneId.of("America/Chicago"));
System.out.println(systemClockTimeAmericaChicago.getZone());
}
America/Chicago
4. public static Clock tickMillis(ZoneId zone)
Example:
This method was introduced in Java 9, it returns the Clock with the current instant ticking in whole milliseconds using the best available system clock.
Clock systemClockTimeAmericaChicago = Clock.tickMillis(ZoneId.of("America/Chicago"));
5. public static Clock tickSeconds(ZoneId zone)
Example:
Clock systemClockTimeAmericaChicago = Clock.tickSeconds(ZoneId.of("America/Chicago"));
6. public static Clock tickMinutes(ZoneId zone)
Example:
Clock systemClockTimeAmericaChicago = Clock.tickMinutes(ZoneId.of("America/Chicago"));
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
- Android : Class file collision: A resource exists with a different case - Android
- The package must have at least one . separator [Android Studio] - Android-Studio
- Comments in Python Programming - Python
- How to see HTTP Request Response Headers in Google Chrome Browser - Chrome
- When should we use Primitive or Wrapper types in Java? - Java
- Java: Print Stack Trace as String Without Exception - Java
- How to redirect SharePoint Site Collection to different URL - SharePoint
- Android Studio emulator/Device logCat logs not displayed - Android-Studio