Date hashCode() method in Java with Examples
Last Updated :
07 Nov, 2019
Improve
The hashCode() method of Java Date class returns a value which is a hash code for this object.
Syntax:
Java
Java
public int hashCode()Parameters: The function does not accept any parameter. Return Value: It returns a hashCode value for this object. Exception: The function does not throws any exception. Program below demonstrates the above mentioned function:
// Java code to demonstrate
// hashCode() function of Date class
import java.util.Date;
import java.util.Calendar;
public class GfG {
// main method
public static void main(String[] args)
{
// creating a Calendar object
Calendar c1 = Calendar.getInstance();
// set Month
// MONTH starts with 0 i.e. ( 0 - Jan)
c1.set(Calendar.MONTH, 11);
// set Date
c1.set(Calendar.DATE, 05);
// set Year
c1.set(Calendar.YEAR, 1996);
// creating a date object with specified time.
Date dateOne = c1.getTime();
System.out.println("Date: " + dateOne);
// Prints hash Code
System.out.println("HashCode: "
+ dateOne.hashCode());
}
}
Output:
Date: Thu Dec 05 08:22:04 UTC 1996 HashCode: -629399711
// Java code to demonstrate
// hashCode() function of Date class
import java.util.Date;
import java.util.Calendar;
public class GfG {
// main method
public static void main(String[] args)
{
// creating a Calendar object
Calendar c1 = Calendar.getInstance();
// set Month
// MONTH starts with 0 i.e. ( 0 - Jan)
c1.set(Calendar.MONTH, 11);
// set Date
c1.set(Calendar.DATE, 21);
// set Year
c1.set(Calendar.YEAR, 1999);
// creating a date object with specified time.
Date dateOne = c1.getTime();
System.out.println("Date: " + dateOne);
// Prints hash Code
System.out.println("HashCode: "
+ dateOne.hashCode());
}
}
Output:
Date: Tue Dec 21 08:22:09 UTC 1999 HashCode: 871724355