Calendar hashCode() Method in Java with Examples

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 paramters.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

The time on Calendar shows: Wed Feb 20 15:55:22 UTC 2019
The hash code for this calendar is: 194416203

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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–



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.