Home »
Java »
Java Reference »
Java LocalDate Class
Java LocalDate Class | equals() Method with Example
LocalDate Class equals() method: Here, we are going to learn about the equals() method of LocalDate Class with its syntax and example.
Submitted by Preeti Jain, on May 29, 2020
LocalDate Class equals() method
- equals() method is available in java.time package.
- equals() method is used to check whether this LocalDate value and the given object value are equal or not.
- equals() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- equals() method does not throw an exception at the time of comparing two objects.
Syntax:
public boolean equals(Object o);
Parameter(s):
- Object o – represents the object to be compared to this LocalDate.
Return value:
The return type of this method is boolean, it returns true when both the compared objects are equal otherwise it returns false.
Example:
// Java program to demonstrate the example
// of boolean equals(Object o) method
// of LocalDate
import java.time.*;
public class EqualsOfLocalDate {
public static void main(String args[]) {
// Instantiates two LocalDate
LocalDate l_da1 = LocalDate.parse("2007-04-04");
LocalDate l_da2 = LocalDate.now();
// Display l_da1,l_da2
System.out.println("LocalDate l_da1 and l_da2: ");
System.out.println("l_da1: " + l_da1);
System.out.println("l_da2: " + l_da2);
System.out.println();
// Here, this method compares this date object
// to the given date object for equality
// it returns false because l_da1 <> l_da2
boolean status = l_da1.equals(l_da2);
// Display status
System.out.println("l_da1.equals(l_da2): " + status);
// Here, this method compares this date object
// to the given date object for equality
// it returns true because l_da1 == l_da1
status = l_da1.equals(l_da1);
// Display status
System.out.println("l_da1.equals(l_da1): " + status);
}
}
Output
LocalDate l_da1 and l_da2:
l_da1: 2007-04-04
l_da2: 2020-05-29
l_da1.equals(l_da2): false
l_da1.equals(l_da1): true