Java Program to check if two dates are equal


The date is a way of keeping track of our time as it is an integral part of our daily lives. In the programming world, there are a few scenarios where we are required to deal with the date and time such as while developing a calendar application and attendance management system in Java. Therefore, Java provides a few built-in classes like Date and LocalDate to work with date and time. In this article, we are going to explore Java programs to check whether two given dates are equal or not.

Java Program to Check if two Dates are Equal

To check whether two dates are equal or not, we need to compare the given dates using built-in methods like 'compareTo()' and 'equals()'. Let's discuss them first.

compareTo()

The Comparable interface defines only a single method named 'CompareTo' that gives the power to compare the objects of a class to itself. It returns 0 when the first date object is equal to the passed object, a positive value if the first date object is greater otherwise a negative value.

Syntax

dateOne.compareTo(dateTwo);

equals()

It is a method of String class which is used to check whether two given strings contain the same set of characters in the same order or not. It returns true if both strings satisfy the condition otherwise false.

Syntax

dateOne.equals(dateTwo);

Now, let's jump into the Java programs to check if two dates are equal or not.

Example 1

In the following example, first, we will use the LocalDate which is an immutable date-time object used to represent Date and its default format is yyyy-MM-dd. Then, we use the equals() method to check if defined dates are equal or not.

import java.time.*;
import java.util.*;
public class Main {  
   public static void main(String[] args) {
      // initializing two unequal dates
      LocalDate dateOne = LocalDate.parse("2021-01-20");
      LocalDate dateTwo = LocalDate.parse("2023-06-01");
      // checking both dates are equal or not
      if(dateOne.equals(dateTwo)) {
         System.out.println("Both dates are equal!");
      } else {
         System.out.println("Both dates are unequal!");
      }
   }
}

Output

Both dates are unequal!

Example 2

In this example, we will use SimpleDateFormat and Date class along with the compareTo() method to check whether two dates are equal or not. Here, the SimpleDateFormat is a class in Java that allows us to convert date to string (formatting) and convert string to date (parsing) in local format. And, the Date is a class that signifies a certain time period (in milliseconds).

import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
   public static void main(String[] args) throws Exception {
      // creating instance of SimpleDateFormat 
      SimpleDateFormat timeformat = new SimpleDateFormat("yy/MM/dd");
      // initializing two dates
      Date dateOne = timeformat.parse("23/06/01");
      Date dateTwo = timeformat.parse("23/06/01");
      // checking both dates are equal or not
      if(dateOne.compareTo(dateTwo) == 0) {
         System.out.println("Both dates are equal");
      } else {
         System.out.println("Both dates are unequal!");
      }
   }
}

Output

Both dates are equal

Example 3

This is another example to check if two dates are equal or not using equals() method.

import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
   public static void main(String[] args) throws Exception {
      // creating instance of SimpleDateFormat 
      SimpleDateFormat timeformat = new SimpleDateFormat("yy/MM/dd");
      // initializing two dates
      Date dateOne = timeformat.parse("23/06/01");
      Date dateTwo = timeformat.parse("23/06/01");
      // checking both dates are equal or not
      if(dateOne.equals(dateTwo)) {
         System.out.println("Both dates are equal");
      } else {
         System.out.println("Both dates are unequal!");
      }
   }
}

Output

Both dates are equal

Conclusion

In this article, we have learned how to compare two dates in Java in order to check whether they are equal or not. For this purpose, we have used two built-in methods named compareTo() and equals(). Also, we discovered how to define dates in our Java programs with the help of LocalDate, SimpleDateFormat and Date class.

Updated on: 10-Aug-2023

151 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements