Java Program to check if two dates are equal

Java 8Object Oriented ProgrammingProgramming

Let us first declare objects of LocalDate −

Date 1

LocalDate dateOne = LocalDate.now();

Date 2

LocalDate dateTwo = LocalDate.of(dateOne.getYear(), dateOne.getMonth(), dateOne.getDayOfMonth());

Let us now use the equals () method to check if both the dates are same or not −

if (dateOne.equals(dateTwo)) {
   System.out.printf("\nBoth the dates are same!", dateOne, dateTwo);
}

Example

import java.time.LocalDate;
public class Demo {
   public static void main(String[] argv) {
      LocalDate dateOne = LocalDate.now();
      LocalDate dateTwo = LocalDate.of(dateOne.getYear(), dateOne.getMonth(), dateOne.getDayOfMonth());
      System.out.printf("Date One = "+dateOne);
      System.out.printf("\nDate Two = "+dateTwo);
      if (dateOne.equals(dateTwo)) {
         System.out.printf("\nBoth the dates are same!", dateOne, dateTwo);
      }
   }
}

Output

Date One = 2019-04-12
Date Two = 2019-04-12
Both the dates are same!
raja
Published on 26-Apr-2019 09:57:22
Advertisements