LocalDate isEqual() method in Java


It can be checked if two LocalDate objects are equal or not using the isEqual() method in the LocalDate class in Java. This method requires a single parameter i.e. the LocalDate object that is to be compared. It returns true if the two LocalDate objects are equal and false otherwise.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.time.*;
public class Main {
   public static void main(String[] args) {
      LocalDate ld1 = LocalDate.parse("2019-02-12");
      LocalDate ld2 = LocalDate.parse("2019-02-12");
      System.out.println("The LocalDate ld1 is: " + ld1);
      System.out.println("The LocalDate ld2 is: " + ld2);
      boolean flag = ld1.isEqual(ld2);
      if(flag)
         System.out.println("
Both LocalDate objects are equal");       else          System.out.println("
Both LocalDate objects are not equal");    } }

Output

The LocalDate ld1 is: 2019-02-12
The LocalDate ld2 is: 2019-02-12
Both LocalDate objects are equal

Now let us understand the above program.

The two LocalDate objects ld1 and ld2 are displayed. It is checked if the LocalDate objects are equal using the isEqual() method. The returned value is displayed using an if statement. A code snippet that demonstrates this is as follows −

LocalDate ld1 = LocalDate.parse("2019-02-12");
LocalDate ld2 = LocalDate.parse("2019-02-12");
System.out.println("The LocalDate ld1 is: " + ld1);
System.out.println("The LocalDate ld2 is: " + ld2);
boolean flag = ld1.isEqual(ld2);
if(flag)
   System.out.println("
Both LocalDate objects are equal"); else    System.out.println("
Both LocalDate objects are not equal");

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

948 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements