javaequalsjava-timelocaldatejsr310

LocalDate: equals vs isEqual


LocalDate in Java has two similar methods equals and isEqual.

What's the difference between them? When do they output different results?


Solution

  • LocalDate.equals, like most other equals method implementations, will always return false if you pass it something other than a LocalDate, even if they represent the same day:

    System.out.println(LocalDate.now().equals(HijrahDate.now())); // false
    

    ChronoLocalDate.isEqual compares whether the two dates are the same day, i.e. the same point on the local time line:

    System.out.println(LocalDate.now().isEqual(HijrahDate.now())); // true