BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Clock class explained with examples

By Chaitanya Singh | Filed Under: java

Java Clock class gives you access to obtain current date-time using a time-zone. Use of Clock class is not mandatory as all date-time classes have now() method which is used to get current date-time information from system in default time-zone. The main purpose of Clock class is to allow access to an alternate clock which can be plugged in an application whenever needed.

Java Clock class:

public abstract class Clock
extends Object

Java Clock class – Method Summary

MethodDescription
boolean equals(Object obj)Checks if this clock is equal to the other specified clock
abstract ZoneId getZone()Gets the time-zone.
int hashCode()Returns hash code for this code.
static Clock fixed(Instant fixedInstant, ZoneId zone)It obtains a clock that always returns the same instant.
long millis()It returns the current millisecond instant of the clock.
static Clock withZone(ZoneId zone)It returns a copy of this clock with the specified time-zone.
static Clock systemUTC()Returns a copy of this clock after converting the date-time in UTC timezone.
static Clock offset(Clock baseClock, Duration offsetDuration)It is used to obtain a clock using specified clock with the specified duration added.
static Clock system(ZoneId zone)Returns a clock that returns the current instant using the system clock.
static Clock tick(Clock baseClock, Duration tickDuration)It obtains a clock that returns the current instant from the specified clock truncated to the nearest occurrence of the specified duration.
static Clock tickMinutes(ZoneId zone)It obtains a clock that returns the current instant ticking in whole minutes using the system clock.
static Clock tickSeconds(ZoneId zone)It obtains a clock that returns the current instant ticking in whole seconds using the system clock.

Java Clock class: getZone(), instant() & millis() methods example

Method getZone() is used to obtain the system default time-zone. The method instant() is used to obtain the current instant and method millis() returns the current milliseconds instant of the clock.

import java.time.Clock;
public class JavaExample {
    public static void main(String[] args) {
        Clock clock = Clock.systemDefaultZone();
        System.out.println(clock.getZone());
        System.out.println(clock.instant());
        System.out.println(clock.millis());
    }
}  

Output:

Asia/Kolkata
2022-06-12T13:13:28.420230Z
1655039608440

Java Clock class: systemUTC() method example

This method returns the clock in the UTC timezone. It obtains the date-time from system clock and converts it into UTC timezone.

import java.time.Clock;
public class JavaExample {
    public static void main(String[] args) {
        Clock clock = Clock.systemUTC();
        System.out.println(clock.instant());
    }
}  

Output:

2022-06-12T13:15:59.036142Z

Java Clock class: offset() method example

The offset() method returns a clock that returns the current instant using specified clock with the specified duration added. Here we have obtained the system default clock using systemDefaultZone() method and then we supplied this clock and duration to the offset method to display the current system instant after adding the specified duration.

import java.time.Clock;
import java.time.Duration;
public class JavaExample {
    public static void main(String[] args) {
        //getting default system clock
        Clock clock = Clock.systemDefaultZone();
        System.out.println("Timezone: "+clock);
        System.out.println("Current Instant: "+clock.instant());
        //duration set to 3 days
        Duration duration = Duration.ofDays(3);
        Clock clock2 = Clock.offset(clock, duration);
        System.out.println(clock2.instant());
    }
}  

Output:

Timezone: SystemClock[Asia/Kolkata]
Current Instant: 2022-06-12T13:27:59.817263Z
2022-06-15T13:27:59.837646Z
❮ Java Tutorial

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap