Skip to content
Related Articles

Related Articles

Java 8 Clock hashCode() method with Examples
  • Last Updated : 10 Dec, 2018

Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8.
hashCode() method of Clock class returns the hash code for this Clock Object. Clocks object overrides this method based on their state. If clock object is not overridden, the behaviour of this method is defined by Object.hashCode(). The hashcode is always the same if the object doesn’t change.
Hashcode is a unique code generated by the JVM at time of object creation. It can be used to perform some operation on hashing related algorithm like hashtable, hashmap etc. An object can also be searched with its unique code (hashcode).

Syntax:

public int hashCode()

Returns: This method returns a suitable hash code for this clock Method.

Example:

Input:: 
a clock class Object e.g Clock.systemDefaultZone()

Output::
HashCode  e.g. 227139178

Explanation:: 
when hashCode() is called, then it will return a hashCode for Class Object. 

Below programs illustrates hashCode() method of java.time.Clock class:



Program 1: Clock obejct is created with systemDefaultZone and using hashCode() of Clock object get the hash Code and print it.




// Java Program to demonstrate 
// hashCode() method of Clock class
  
import java.time.*;
  
// create class
public class hashCodeMethodDemo {
  
    // Main method
    public static void main(String[] args)
    {
  
        // create Clock Object
        Clock clock = Clock.systemDefaultZone();
  
        // get hash Code of Clock
        // object using hashCode() method
        int code = clock.hashCode();
  
        // print details of TimeZone
        System.out.println("hash Code for class "
                           + clock + " is " + code);
    }
}


Output:

hash Code for class SystemClock[Etc/UTC] is 227139178

Program 2: Get HashCode of Clock object with Zone “Asia/calcutta” using hashCode()




// Java Program to demonstrate 
// hashCode() method of Clock class
  
import java.time.*;
  
// create class
public class hashCodeMethodDemo {
  
    // Main method
    public static void main(String[] args)
    {
  
        // create a Zone Id for Calcutta
        ZoneId zoneId = ZoneId.of("Asia/Calcutta");
  
        // create Clock Object by passing zoneID
        Clock clock = Clock.system(zoneId);
  
        // get hash Code of Clock
        // object using hashCode() method
        int code = clock.hashCode();
  
        // print details of TimeZone
        System.out.println("hashCode for clock object "
                           + clock + " is " + code);
    }
}


Output:

hashCode for clock object SystemClock[Asia/Calcutta] is -681304889

Reference:
https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#hashCode–

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :