Compare Date Without Time In Java

Compare Date

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;

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 Onward

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;

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 Date Comparison

Execute the above class and now let’s see what output displayed in the console:

Java up to 7

Start date [Wed Nov 15 17:16:08 IST 2023] and end date [Wed Nov 15 17:16:08 IST 2023] are equal
Start date [Sat Feb 11 00:00:00 IST 2023] is before end date [Sun Feb 12 00:00:00 IST 2023]
Start date [Sat Feb 11 00:00:00 IST 2023] is after end date [Sat Feb 11 00:00:00 IST 2023]

Java 8 or later

Start date [2023-11-15] and end date [2023-11-15] are equal
Start date [2023-02-11] is before end date [2023-02-12]
Start date [2023-02-11] is after end date [2023-02-11]

That’s all about comparing two dates without time in Java.

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *