Java Program to compare dates if a date is after another date


In the world of Java programming, there are a few scenarios where we are required to deal with the date and time such as while developing a calendar application, attendance management system in Java and checking age of two persons. Also, the date is a way of keeping track of our time as it is an integral part of our daily lives. Therefore, Java provides classes like Date and LocalDate to work with date and time. And, to compare and check if a date is after another or not, it provides a few useful built-in methods like 'compareTo()' and 'after()'.

Java Program to compare dates if a date is after another date

Before writing the Java programs to compare two dates, it is necessary to understand the problem statement with the help of an example.

Instance

Input 1

dateOne = "2023-01-20";
dateTwo = "2024-01-20";

Output

dateTwo is after dateOne

Input 2

dateOne = "2023-06-20";
dateTwo = "2023-01-20";

Output

dateOne is after dateTwo

As mentioned in the previous section, we can use the 'compareTo()' and 'after()' methods to compare and check whether a date is after another or not. Now, let's discuss these methods and then, we will jump into their practical implementation.

after()

The after() is a method of Calendar class which is used to identify if a specified date comes after the passed date. It accepts a date as an argument and returns true when the first date object is after the passed date object otherwise false.

Syntax

dateOne.after(dateTwo);

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);

Example 1

In the following example, we will use the Calendar class of 'java.util' package which is a class that provides a calendar system in Java. And, to check if the first date is after second date, we will use the 'after()' method along with the if-else block.

import java.util.Calendar;
public class Example1 {
   public static void main(String[] args) {
      // creating two instances of Calendar class
      Calendar dateOne = Calendar.getInstance();
      Calendar dateTwo = Calendar.getInstance();
      // setting date, month and year for first instance
      dateOne.set(Calendar.YEAR, 2024);
      dateOne.set(Calendar.MONTH, 07);
      dateOne.set(Calendar.DATE, 25);
      System.out.println("The date one is: 25 July 2024");
      // setting date, month and year for second instance
      dateTwo.set(Calendar.YEAR, 2023);
      dateTwo.set(Calendar.MONTH, 07);
      dateTwo.set(Calendar.DATE, 25);
      System.out.println("The date two is: 25 July 2023");
      // checking if dateOne is after dateTwo
      if(dateOne.after(dateTwo)) {
         System.out.println("Date One is after Date Two!");
      } else {
         System.out.println("Date One is before Date Two!");
      }
   }
}

Output

The date one is: 25 July 2024
The date two is: 25 July 2023
Date One is after Date Two!

Example 2

In this example, we will use SimpleDateFormat and Date class along with the compareTo() method to compare and check whether the first date is after second date 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 Example2 {
   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");
      System.out.println("The date one is: " + dateOne);
      Date dateTwo = timeformat.parse("23/07/01");
      System.out.println("The date two is: " + dateTwo);
      // checking if dateOne is after dateTwo
      if(dateOne.compareTo(dateTwo) == 1) {
         System.out.println("Date One is after Date Two!");
      } else {
         System.out.println("Date One is before Date Two!");
      }
   }
}

Output

The date one is: Thu Jun 01 00:00:00 GMT 2023
The date two is: Sat Jul 01 00:00:00 GMT 2023
Date One is before Date Two!

Conclusion

In this article, we have learned how to compare two dates in Java in order to check whether one date is after another or not. For this purpose, we have used two built-in methods named compareTo() and after(). Also, we discovered how to define dates in our Java programs with the help of SimpleDateFormat and Calendar class.

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 10-Aug-2023

281 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements