Introduction
This tutorial shows how to compare date without time in Java between two dates. Situation may arise when you need to compare two dates in your Java applications and if you need to compare only date part without time part for accurate results then you need to first remove the time part from the actual date and perform caparison until Java 8.
In Java 8 you can easily compare two dates without having time because Java 8 Date API provides LocalDate
final class that gives you only date part. So using LocalDate
object you can use methods, such as, isBefore()
, isAfter()
to compare your date part only.
Here is an example that will show you how to remove time part from the actual date and perform comparison for Java version up to 7 as well as Java version 8 or later. This example will tell you if one date is before, after or equal to another date.
Prerequisites
Java
Compare Date up to Java 7
In the below comparison code example I retrieve only the date part using Java’s Calendar
instance. So I set 0 for time part in the calendar instance and return the date. Then I check whether one date is after, before or equal to another date.
package com.roytuts.java.compare.date.without.time;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class CompareDateWithoutTime {
public static void main(String[] args) {
// Test 1
String response = CompareDateWithoutTime.compareTwoDates(new Date(), new Date());
System.out.println(response);
// Test 2
Date startDate = new GregorianCalendar(2014, Calendar.FEBRUARY, 11).getTime();
Date endDate = new GregorianCalendar(2014, Calendar.FEBRUARY, 12).getTime();
response = CompareDateWithoutTime.compareTwoDates(startDate, endDate);
System.out.println(response);
// Test 3
startDate = new GregorianCalendar(2014, Calendar.FEBRUARY, 12).getTime();
endDate = new GregorianCalendar(2014, Calendar.FEBRUARY, 11).getTime();
response = CompareDateWithoutTime.compareTwoDates(startDate, endDate);
System.out.println(response);
}
public static String compareTwoDates(Date startDate, Date endDate) {
Date sDate = getZeroTimeDate(startDate);
Date eDate = getZeroTimeDate(endDate);
if (sDate.before(eDate)) {
return "Start date [" + startDate + "] is before end date [" + endDate + "]";
}
if (sDate.after(eDate)) {
return "Start date [" + endDate + "] is after end date [" + endDate + "]";
}
return "Start date [" + startDate + "] and end date [" + endDate + "] are equal";
}
private static Date getZeroTimeDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
date = calendar.getTime();
return date;
}
}
Compare Date in Java 8 or later
So using Java version 8 or later, it is very easy to compare the dates value only.
package com.roytuts.java.compare.date.without.time;
import java.time.LocalDate;
import java.time.Month;
public class CompareDateWithoutTimeJava8 {
public static void main(String[] args) {
// Test 1
String response = CompareDateWithoutTimeJava8.compareTwoDates(LocalDate.now(), LocalDate.now());
System.out.println(response);
// Test 2
LocalDate startDate = LocalDate.of(2014, Month.FEBRUARY, 11);
LocalDate endDate = LocalDate.of(2014, Month.FEBRUARY, 12);
response = CompareDateWithoutTimeJava8.compareTwoDates(startDate, endDate);
System.out.println(response);
// Test 3
startDate = LocalDate.of(2014, Month.FEBRUARY, 12);
endDate = LocalDate.of(2014, Month.FEBRUARY, 11);
response = CompareDateWithoutTimeJava8.compareTwoDates(startDate, endDate);
System.out.println(response);
}
public static String compareTwoDates(LocalDate startDate, LocalDate endDate) {
if (startDate.isBefore(endDate)) {
return "Start date [" + startDate + "] is before end date [" + endDate + "]";
}
if (startDate.isAfter(endDate)) {
return "Start date [" + endDate + "] is after end date [" + endDate + "]";
}
return "Start date [" + startDate + "] and end date [" + endDate + "] are equal";
}
}
Testing the Program
Execute the above class and now let’s see what output displayed in the console:
Java up to 7
Start date [Mon Aug 17 15:31:20 IST 2020] and end date [Mon Aug 17 15:31:20 IST 2020] are equal
Start date [Tue Feb 11 00:00:00 IST 2014] is before end date [Wed Feb 12 00:00:00 IST 2014]
Start date [Tue Feb 11 00:00:00 IST 2014] is after end date [Tue Feb 11 00:00:00 IST 2014]
Java 8 or later
Start date [2020-08-17] and end date [2020-08-17] are equal
Start date [2014-02-11] is before end date [2014-02-12]
Start date [2014-02-11] is after end date [2014-02-11]
That’s all about comparing two dates without time in Java.