
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compare Dates in Java: Check If One Date Is After Another
In Java, there are a few scenarios where we are required to work 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. In this article, we will discuss how to compare and check if a date is after another or not.
Example Scenario 1
Input: dateOne = "2023-01-20"; dateTwo = "2024-01-20"; Output: dateTwo is after dateOne
Example Scenario 2
Input: dateOne = "2023-06-20"; dateTwo = "2023-01-20"; Output: dateOne is after dateTwo
Comparing dates in Java
To compare and check if a date is after another or not, Java provides a few useful built-in methods ?
- after() method
- compareTo() method
Using after() method
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);
Example
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!"); } } }
On running the above code, it will produce the following result ?
The date one is: 25 July 2024 The date two is: 25 July 2023 Date One is after Date Two!
Using compareTo() method
The Comparable interface defines only a single method named CompareTo that is used 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
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!"); } } }
On executing this code, you will see the below 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.