
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"));
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
- JdbcTemplate Batch Insert Example using Spring Boot - Java
- How to know if you have blocked your friend Number on Android Phone - Android
- Google Local Guide Program and Perks of Contributing to Google Maps - Google
- How to turn off Automatically adjust brightness on Mac Ventura 13 - MacOS
- Bash How to Save Output of a Command to a Variable - Bash
- How to Refresh Mac Desktop - MacOS
- Java 8 - Convert List to Map Examples - Java
- Bootstrap tooltip not working - Bootstrap