The equals() method compares two dates with each other and returns boolean value, true and false based on the comparison. We can also use compareTo() method for the same purpose, however compareTo() returns int value instead of boolean value.
Method Signature:
boolean equals(Object obj)
Checks if this date is equal to another date. It returns true if the dates are equal else it returns false.
LocalDate equals() example
import java.time.LocalDate; public class Example { public static void main(String[] args) { // Comparing current date with the given date LocalDate currentDate = LocalDate.now(); System.out.println(currentDate); LocalDate givenDate = LocalDate.of(2017, 10, 28); System.out.println(givenDate); System.out.println(currentDate.equals(givenDate)); /* Comparing the dates given as Strings. Strings are parsed * into LocalDate instances and then compared against each other */ LocalDate date1 = LocalDate.parse("2017-10-21"); System.out.println(date1); LocalDate date2 = LocalDate.parse("2017-08-23"); System.out.println(date2); System.out.println(date1.equals(date2)); } }
Output:
2017-10-28 2017-10-28 true 2017-10-21 2017-08-23 false
Leave a Reply