LAST UPDATED: JUNE 15, 2020
Java LocalDate equals() Method
The equals()
method is used to check if this date is equal to another date. It takes a single argument of Object
class type which is actually a date that we want to compare. The syntax of the method is given below.
Syntax
public boolean equals(Object obj)
Parameters
obj - Another date to check equal
Return
It returns true if date is equal, fale otherwise.
Example:
import java.time.LocalDate;
public class Demo {
public static void main(String[] args){
// Take a date
LocalDate localDate1 = LocalDate.of(2018, 2, 20);
// Displaying date
System.out.println("First Date is : "+localDate1);
// Take another date
LocalDate localDate2 = LocalDate.of(2018, 2, 22);
// Displaying date
System.out.println("Another Date is : "+localDate2);
// Using equals() method
boolean val = localDate2.equals(localDate1);
System.out.println(val);
}
}
First Date is : 2018-02-20
Another Date is : 2018-02-22
false
Example:
import java.time.LocalDate;
public class Demo {
public static void main(String[] args){
// Take a date
LocalDate localDate1 = LocalDate.of(2018, 2, 20);
// Displaying date
System.out.println("First Date is : "+localDate1);
// Take another date
LocalDate localDate2 = LocalDate.of(2018, 2, 20);
// Displaying date
System.out.println("Another Date is : "+localDate2);
// Using equals() method
boolean val = localDate2.equals(localDate1);
System.out.println(val);
}
}
First Date is : 2018-02-20
Another Date is : 2018-02-20
true
Live Example:
Try with a live example, execute the code instantly with our powerful Online Java Compiler.