Java Program to Compare Dates Using the Date.Equals() Method

You are currently viewing Java Program to Compare Dates Using the Date.Equals() Method

Java program to compare dates using the Date.equals() method

// Java program to compare dates using 
// Date.equals() method

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Date date1 = new Date(22, 9, 16);
    Date date2 = new Date(21, 10, 15);
    Date date3 = new Date(21, 10, 15);

    boolean result;

    result = date1.equals(date2);
    if (result == true)
      System.out.println("Both are equal");
    else
      System.out.println("The date1 and date2 are not equal");

    result = date2.equals(date3);
    if (result == true)
      System.out.println("Both are equal");
    else
      System.out.println("The date2 and date3 are not equal");
  }
}

Output

The date1 and date2 are not equal
Both are equal

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply