Overview
In this tutorial, we show you how to compare dates in Java. There are 4 ways to compare two dates in Java: java.util.Date.compareTo() method, Calender.before() (Calender.after() and Calender.equals()) method, Date.before() (Date.after() and Date.equals()), LocalDate.isAfter() (LocalDate.isBefore(), LocalDate.isEqual()) method.Using Date.before(), Date.after() and Date.equals()
- The Date1.after(Date2) return true if the Date1 is after Date2, false otherwise.
- The Date1.before(Date2) return true if the Date1 is before Date2, false otherwise.
- The Date1.equals(Date2) return true if the Date1 is equal Date2, false otherwise.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2018-06-30");
Date date2 = sdf.parse("2018-05-31");
if (date1.after(date2)) {
System.out.println("Date1 is greater than Date2");
}
if (date1.before(date2)) {
System.out.println("Date1 is less than Date2");
}
if (date1.equals(date2)) {
System.out.println("Date1 is equal Date2");
}
Using Java.util.Date.compareTo() method
The date1.compareTo(date2) return 0 if the Date1 is equal Date2The date1.compareTo(date2) return a value less than 0 if the Date1 is before Date2
The date1.compareTo(date2) return a value greater than 0 if the Date1 is after Date2
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2018-06-30");
Date date2 = sdf.parse("2018-05-31");
if (date1.compareTo(date2) > 0) {
System.out.println("Date1 is greater than Date2");
} else if (date1.compareTo(date2) < 0) {
System.out.println("Date1 is less than Date2");
} else if (date1.compareTo(date2) == 0) {
System.out.println("Date1 is equal to Date2");
}
Using Calender.before(), Calender.after() and Calender.equals() method
- The cal1.after(cal2) return true if the Date1 is after Date2, false otherwise.
- The cal1.before(cal2) return true if the Date1 is before Date2, false otherwise.
- The cal1.equals(cal2) return true if the Date1 is equal Date2, false otherwise.
Date date1 = sdf.parse("2018-06-30");
Date date2 = sdf.parse("2018-05-31");
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
if (cal1.after(cal2)) {
System.out.println("Date1 is greater than Date2");
}
if (cal1.before(cal2)) {
System.out.println("Date1 is less than Date2");
}
if (cal1.equals(cal2)) {
System.out.println("Date1 is equal Date2");
}
Using LocalDate.isAfter(), LocalDate.isBefore(), LocalDate.isEqual() method for Java 8 and later
- The localDate1.isAfter(localDate2) return true if the Date1 is after Date2, false otherwise.
- The localDate1.isBefore(localDate2) return true if the Date1 is before Date2, false otherwise.
- The localDate1.isEqual(localDate2) return true if the Date1 is equal Date2, false otherwise.
LocalDate localDate1 = LocalDate.of(2018, 06, 30);
LocalDate localDate2 = LocalDate.of(2018, 05, 31);
if (localDate1.isAfter(localDate2)) {
System.out.println("Date1 is greater than Date2");
}
if (localDate1.isBefore(localDate2)) {
System.out.println("Date1 is less than Date2");
}
if (localDate1.isEqual(localDate2)) {
System.out.println("Date1 is equal Date2");
}
CompareDateExample
package com.jackrutorial;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
public class CompareDateExample {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2018-06-30");
Date date2 = sdf.parse("2018-05-31");
//using Date.before(), Date.after() and Date.equals()
System.out.println("### Using Date.before(), Date.after() and Date.equals()");
System.out.println("Date1 : " + sdf.format(date1));
System.out.println("Date2 : " + sdf.format(date2));
if (date1.after(date2)) {
System.out.println("Date1 is greater than Date2");
}
if (date1.before(date2)) {
System.out.println("Date1 is less than Date2");
}
if (date1.equals(date2)) {
System.out.println("Date1 is equal Date2");
}
System.out.println("### Using compareTo");
System.out.println("Date1 : " + sdf.format(date1));
System.out.println("Date2 : " + sdf.format(date2));
if (date1.compareTo(date2) > 0) {
System.out.println("Date1 is greater than Date2");
} else if (date1.compareTo(date2) < 0) {
System.out.println("Date1 is less than Date2");
} else if (date1.compareTo(date2) == 0) {
System.out.println("Date1 is equal to Date2");
}
//Calender.before(), Calender.after() and Calender.equals()
System.out.println("### Using Calender.before(), Calender.after() and Calender.equals()");
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
System.out.println("Date1 : " + sdf.format(date1));
System.out.println("Date2 : " + sdf.format(date2));
if (cal1.after(cal2)) {
System.out.println("Date1 is greater than Date2");
}
if (cal1.before(cal2)) {
System.out.println("Date1 is less than Date2");
}
if (cal1.equals(cal2)) {
System.out.println("Date1 is equal Date2");
}
//Update for Java 8 and later
LocalDate localDate1 = LocalDate.of(2018, 06, 30);
LocalDate localDate2 = LocalDate.of(2018, 05, 31);
System.out.println("date1 : " + sdf.format(date1));
System.out.println("date2 : " + sdf.format(date2));
if (localDate1.isAfter(localDate2)) {
System.out.println("Date1 is greater than Date2");
}
if (localDate1.isBefore(localDate2)) {
System.out.println("Date1 is less than Date2");
}
if (localDate1.isEqual(localDate2)) {
System.out.println("Date1 is equal Date2");
}
}
}
Console Output
### Using Date.before(), Date.after() and Date.equals() Date1 : 2018-06-30 Date2 : 2018-05-31 Date1 is greater than Date2 ### Using compareTo Date1 : 2018-06-30 Date2 : 2018-05-31 Date1 is greater than Date2 ### Using Calender.before(), Calender.after() and Calender.equals() Date1 : 2018-06-30 Date2 : 2018-05-31 Date1 is greater than Date2 date1 : 2018-06-30 date2 : 2018-05-31 Date1 is greater than Date2
