🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
This article is the series of Java Date Time API Tutorial. In this article, we will discuss important methods or APIs of the Java Clock class from the java. time package.
Learn and master in Java 8 features at Java 8 Tutorial with Examples.
The Clock class was added in Java 8 and provides access to an instant in time using the best available system clock, and to be used as a time provider which can be effectively stubbed for testing purposes.
Instances of this class are used to find the current instant, which can be interpreted using the stored time-zone to find the current date and time. As such, a clock can be used instead of System.currentTimeMillis() and TimeZone.getDefault().
Clock Class Diagram
The below class diagram shows a list of methods that the Clock class provides.
Java Clock Class Methods/APIs with Examples
Let's explore a few important and commonly used Clock methods with examples.
instant()
This method is used to get the current instant of the clock
Clock clock = Clock.systemDefaultZone();
Clock clock1 = Clock.systemUTC();
System.out.println("Clock 1 Instant: " + clock.instant());
System.out.println("Clock 2 Instant: " + clock1.instant());
Output:
Clock 1 Instant: 2018-12-22T07:20:26.681Z
Clock 2 Instant: 2018-12-22T07:20:26.704Z
fixed()
This method returns a clock that always returns the same instant. The main use case for this method is in testing, where the fixed clock ensures that tests are not dependent on the current clock.
Clock clock = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault());
Clock clock1 = Clock.fixed(Instant.ofEpochSecond(3600), ZoneId.systemDefault());
System.out.println("Clock 1: " + clock.toString());
System.out.println("Clock 2: " + clock1.toString());
Output:
Clock 1: FixedClock[1970-01-01T01:00:00Z,Asia/Calcutta]
Clock 2: FixedClock[1970-01-01T01:00:00Z,Asia/Calcutta]
withZone()
This method returns a copy of this clock with a different time-zone.
Example: Below example demonstrates the usage of withZone() method with default zone.
Clock clock = Clock.systemUTC();
Clock clock1 = clock.withZone(ZoneId.systemDefault());
System.out.println("Clock : " + clock.instant());
System.out.println("Clock1 : " + clock1.instant());
Output:
Clock : 2018-12-22T07:06:19.348Z
Clock1 : 2018-12-22T07:06:19.370Z
tickSeconds()
This static method returns the current instant ticking in whole seconds for the given time zone. This clock will always have the nano-of-second field set to zero:
Clock clock = Clock.systemDefaultZone();
Clock clock1 = Clock.tickSeconds(ZoneId.systemDefault());
System.out.println("Clock : " + clock.instant());
System.out.println("Clock1 : " + clock1.instant());
Output:
Clock : 2018-12-22T07:08:50.202Z
Clock1 : 2018-12-22T07:08:50Z
tickMinutes()
This method used to obtain a clock that returns the current instant ticking in whole minutes using best available system clock.
Example:
Clock clock = Clock.systemDefaultZone();
Clock clock1 = Clock.tickMinutes(ZoneId.systemDefault());
System.out.println("Clock : " + clock.instant());
System.out.println("Clock1 : " + clock1.instant());
Output:
Clock : 2018-12-22T07:11:18.229Z
Clock1 : 2018-12-22T07:11:00Z
tick()
This static method returns instants from the specified clock rounded to the nearest occurrence of the specified duration. The specified clock duration must be positive:
Clock clock = Clock.systemUTC();
Duration tickDuration = Duration.ofNanos(250000);
Clock clock1 = Clock.tick(clock, tickDuration);
System.out.println("Clock : " + clock.instant());
System.out.println("Clock1 : " + clock1.instant());
Output:
Clock : 2018-12-22T07:11:52.714Z
Clock1 : 2018-12-22T07:11:52.819Z
systemUTC()
This method returns a Clock object representing the current instant in the UTC zone:
Clock clock = Clock.systemUTC();
System.out.println("Clock : " + clock.instant());
Output:
Clock : 2018-12-22T07:14:04.040Z
systemDefaultZone()
This static method returns a Clock object representing the current instant and using the default time zone of the system it’s running on:
Clock clock = Clock.systemDefaultZone();
System.out.println("Clock : " + clock.toString());
Output:
Clock : SystemClock[Asia/Calcutta]
system()
Obtains a clock that returns the current instant using best available system clock.
Clock clock = Clock.system(ZoneId.systemDefault());
System.out.println("Clock : " + clock.instant());
Output:
Clock : 2018-12-22T07:17:37.418Z
offset()
Obtains a clock that returns instants from the specified clock with the specified duration added
Clock clock = Clock.systemUTC();
Duration duration = Duration.ofHours(5);
Clock clock1 = Clock.offset(clock, duration);
System.out.println("Clock 1: " + clock.instant());
System.out.println("Clock 2: " + clock1.instant());
Output:
Clock 1: 2018-12-22T07:18:36.695Z
Clock 2: 2018-12-22T12:18:36.800Z
millis()
This method is used to get the current millisecond instant of the clock.
Clock clock = Clock.systemDefaultZone();
Clock clock1 = Clock.systemUTC();
System.out.println("Clock 1 millis: " + clock.millis());
System.out.println("Clock 2 millis: " + clock1.millis());
Output:
Clock 1 millis: 1545463167415
Clock 2 millis: 1545463167415
getZone()
Gets the time-zone being used to create dates and times.
Clock clock = Clock.systemDefaultZone();
Clock clock1 = Clock.systemUTC();
System.out.println("Clock 1 Zone: " + clock.getZone());
System.out.println("Clock 2 Zone: " + clock1.getZone());
Output:
Clock 1 Zone: Asia/Calcutta
Clock 2 Zone: Z
Related Java 8 Date & Time API Guide
References
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business

Comments
Post a Comment
Leave Comment