LAST UPDATED: NOVEMBER 24, 2020
Java LocalDate hashCode() Method With Examples
Java hashCode()
method is used to get the hash code of a date. Hash code is an integer number that is assigned by the JVM to an object. Each time when we create an object of LocalDate
JVM creates a hashcode number and by using hashCode()
method we can get that integer value.
Java hashCode()
method does not take any argument and returns an integer value. The syntax of the method is given below.
Syntax
public int hashCode()
Returns:
It returns an integer value.
Time for an Example:
Let's take an example to get the hash code of a date object. In this example, we are using the hashCode()
method that results in an integer hashcode value.
import java.time.LocalDate;
public class DateDemo {
public static void main(String[] args){
LocalDate localDate = LocalDate.of(2015, 10, 21);
System.out.println(localDate);
int hashcode = localDate.hashCode();
System.out.println("hashcode of the above date : "+hashcode);
}
}
2015-10-21
hashcode of the above date : 4127381
Time for another Example
Let's take another to understand the hashCode()
method. Here we are getting hashcode of the current date, the current date is fetched by now()
method. See the below example.
import java.time.LocalDate;
public class DateDemo {
public static void main(String[] args){
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
int hashcode = localDate.hashCode();
System.out.println("hashcode of current date : "+hashcode);
}
}
2020-06-05
hashcode of current date : 4137349
Live Example:
Try with a live example, execute the code instantly with our powerful Online Java Compiler.