Home > Java > javaTutorial > Java program to check if two dates are equal

Java program to check if two dates are equal

WBOY
Release: 2023-09-17 17:41:05
forward
1058 people have browsed it

Java program to check if two dates are equal

Date is a way of recording time because it is an integral part of our daily lives. In the programming world, there are some scenarios that require us to deal with date and time, such as developing calendar applications and attendance management systems in Java. Therefore, Java provides some built-in classes such as Date and LocalDate to handle dates and times. In this article, we will explore Java program to check if two given dates are equal.

Java program to check if two dates are equal

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

compareTo()

The Comparable interface only defines a method named "CompareTo", which provides the function of comparing an object of a class with 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, and a negative value otherwise.

grammar

dateOne.compareTo(dateTwo);
Copy after login

equal()

It is a method of the String class that checks whether two given strings contain the same set of characters in the same order. Returns true if both strings satisfy the condition, false otherwise.

grammar

dateOne.equals(dateTwo);
Copy after login

Now, let us enter the Java program to check if two dates are equal.

Example 1

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

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!");
      }
   }
}
Copy after login

Output

Both dates are unequal!
Copy after login

Example 2

In this example, we will use SimpleDateFormat and Date class with compareTo() method to check if two dates are equal. Here, SimpleDateFormat is a class in Java that allows us to convert date to string (formatting) and convert string to date in native format (parsing). And, Date is a class that represents a specific period of time 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!");
      }
   }
}
Copy after login

Output

Both dates are equal
Copy after login
Copy after login

Example 3

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

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!");
      }
   }
}
Copy after login

Output

Both dates are equal
Copy after login
Copy after login

in conclusion

In this article, we learned how to compare two dates in Java to check if they are equal. To do this, we use two built-in methods called compareTo() and equals(). Furthermore, we also saw how to define dates in Java programs with the help of LocalDate, SimpleDateFormat and Date classes.

The above is the detailed content of Java program to check if two dates are equal. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template