Java

Java Clock – HashCode()

When you are working with clock objects, in some cases, you need to share the clock information securely to another person via an external application. In this tutorial, we will see how to convert the clock information into a Hash code using the hashCode() method in Java. With this, the attacker/hacker does not recognize what exactly is the clock information that you are sending.

Let’s explore this method in detail.

Java.time.Clock.hashCode()

The java.time.Clock.hashCode() method in Java clock is used to return the hash code in the integer format from the clock object. It doesn’t take any parameters.

Syntax

clock_object.hashCode()

Where the clock_object represents the clock that has clock information.

We will discuss the different examples with different TimeZones.

Example 1: With Default Zone

Let’s create a clock object with default zone using the systemDefaultZone() and return the hashCode.

import java.time.Clock;

public class Main
{
    public static void main(String[] args) {

        // Create the clock object named clock_object
        Clock clock_object = Clock.systemDefaultZone();
       
        System.out.println("Clock Details: "+clock_object);
 
        // Return the hash code for the above clock object.
        System.out.println("Hash Code: "+clock_object.hashCode());
         
        }
}

Output

The default Zone is GMT and the secure key/Hash code is 70703.

Explanation
Line 8: Create the clock object named clock_object with the default zone.

Line 10: Return the clock details by displaying the clock object.

Line 13: Return the Hash code of the clock object.

Example 2: With Particular Zones

Let’s create three clock objects separately with three different zones using the system() and return the hashCode.

import java.time.*;

public class Main
{
    public static void main(String[] args) {

        // Create the clock object named clock_object1 with ZoneId - Colombo
        Clock clock_object1 = Clock.system(ZoneId.of("Asia/Colombo"));
 
        // Return the hash code for clock object1.
        System.out.println("Hash Code for "+clock_object1+" is: " +clock_object1.hashCode());
       
        // Create the clock object named clock_object2 with ZoneId - New_York
        Clock clock_object2 = Clock.system(ZoneId.of("America/New_York"));
 
        // Return the hash code for clock object2.
        System.out.println("Hash Code for "+clock_object2+" is: " +clock_object2.hashCode());
       
         // Create the clock object named clock_object3 with ZoneId - Egypt
        Clock clock_object3 = Clock.system(ZoneId.of("Egypt"));
 
        // Return the hash code for clock object3.
        System.out.println("Hash Code for "+clock_object3+" is: " +clock_object3.hashCode());
         
        }
}

Output

We created three zones – Asia/Colombo, America/New_York and Egypt. Finally, we return the Hash codes for these three zones.

Explanation
Line 8: Create the clock object named clock_object1 with the zone – Colombo.

Line 11: Return the Hash code for clock_object1.

Line 14: Create the clock object named clock_object2 with Zone – New_York.

Line 17: Return the Hash code for clock_object2.

Line 20: Create the clock object named clock_object3 with Zone – Egypt.

Line 23: Return the Hash code for clock_object3.

Example 3: With Particular Zone TimeStamps

Let’s create three clock objects separately with three different zones using the system() and return the hashCode for those clock timestamps through the instant() method.

import java.time.*;

public class Main
{
    public static void main(String[] args) {

        // Create the clock object named clock_object1 with ZoneId - Colombo
        Clock clock_object1 = Clock.system(ZoneId.of("Asia/Colombo"));
 
        // Return the hash code for clock object1 TimeStamp.
        System.out.println("Hash Code for the TimeStamp- "+clock_object1.instant()+" is: " +clock_object1.instant().hashCode());
       
        // Create the clock object named clock_object2 with ZoneId - New_York
        Clock clock_object2 = Clock.system(ZoneId.of("America/New_York"));
 
        // Return the hash code for clock object2 TimeStamp.
        System.out.println("Hash Code for the TimeStamp- "+clock_object2.instant()+" is: " +clock_object2.instant().hashCode());
       
         // Create the clock object named clock_object3 with ZoneId - Egypt
        Clock clock_object3 = Clock.system(ZoneId.of("Egypt"));
 
        // Return the hash code for clock object3 TimeStamp.
        System.out.println("Hash Code for the TimeStamp- "+clock_object3.instant()+" is: " +clock_object3.instant().hashCode());
         
        }
}

Output

We created three zones – Asia/Colombo, America/New_York and Egypt. Finally, we return the Hash codes for these three zone timestamps.

Explanation
Line 8: Create the clock object named clock_object1 with the zone – Colombo.

Line 11: Return the Hash code for clock_object1 timestamp.

Line 14: Create the clock object named clock_object2 with Zone – New_York.

Line 17: Return the Hash code for clock_object2 timestamp.

Line 20: Create the clock object named clock_object3 with the zone – Egypt.

Line 23: Return the Hash code for clock_object3 timestamp.

Conclusion

At the end of this tutorial, we understood how to return the Hash code of a particular clock object using the hashCode() method. This method is available from the Java-8 version and onwards. It can also be possible to apply the hashCode() along with the instant() method.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain