Skip to content
Related Articles

Related Articles

Improve Article

Calendar hashCode() Method in Java with Examples

  • Last Updated : 05 Jun, 2021

The hashCode() method in Calendar class is used to return the hash code for this Calendar Object.
Syntax: 
 

public int hashCode()

Parameters: The method does not take any parameters.
Return Value: The method returns the hash code value for this calendar object..
Below programs illustrate the working of hashCode() Method of Calendar class: 
Example 1: 
 

Java




// Java code to illustrate
// hashCode() method
 
import java.util.*;
 
public class Calendar_Demo {
    public static void main(String args[])
    {
        // Creating a calendar object
        Calendar calndr = Calendar.getInstance();
 
        // Displaying the current calendar
        System.out.println("The time on"
                           + " Calendar shows: "
                           + calndr.getTime());
 
        // Getting the hash code
        int hash_code = calndr.hashCode();
        System.out.println("The hash code "
                           + "for this calendar is: "
                           + hash_code);
    }
}
Output: 
The time on Calendar shows: Wed Feb 20 15:55:22 UTC 2019
The hash code for this calendar is: 194416203

 

Example 2: 
 

Java




// Java code to illustrate
// hashCode() method
 
import java.util.*;
 
public class Calendar_Demo {
    public static void main(String args[])
    {
        // Creating a calendar
        Calendar calndr
            = new GregorianCalendar(2018, 6, 10);
 
        // Displaying the current calendar
        System.out.println("The time on"
                           + " Calendar shows: "
                           + calndr.getTime());
 
        // Getting the hash code
        int hash_code = calndr.hashCode();
        System.out.println("The hash code "
                           + "for this calendar is: "
                           + hash_code);
    }
}
Output: 
The time on Calendar shows: Tue Jul 10 00:00:00 UTC 2018
The hash code for this calendar is: -2123101761

 

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.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. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :