Tools

How do I checks if two dates are on the same day?

In this example you’ll learn how to find out if two defined date objects are on the same day. It means that we are only interested in the date information and ignoring the time information of these date objects. We’ll be using an API provided by the Apache Commons Lang in this example. So here is the code snippet:

package org.kodejava.example.commons.lang;
import org.apache.commons.lang.time.DateUtils;
import java.util.Calendar;
import java.util.Date;
public class CheckSameDay {
public static void main(String[] args) {
Date date1 = new Date();
Date date2 = new Date();
//
// Checks to see if the dates is on the same day.
//
if (DateUtils.isSameDay(date1, date2)) {
System.out.printf("%1$te/%1$tm/%1$tY and %2$te/%2$tm/%2$tY " +
"is on the same day.%n", date1, date2);
}
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
//
// Checks to see if the calendars is on the same day.
//
if (DateUtils.isSameDay(cal1, cal2)) {
System.out.printf("%1$te/%1$tm/%1$tY and %2$te/%2$tm/%2$tY " +
"is on the same day.%n", cal1, cal2);
}
cal2.add(Calendar.DAY_OF_MONTH, 10);
if (!DateUtils.isSameDay(cal1, cal2)) {
System.out.printf("%1$te/%1$tm/%1$tY and %2$te/%2$tm/%2$tY " +
"is not on the same day.", cal1, cal2);
}
}
}

The example result produced by this snippet are:

15/06/2012 and 15/06/2012 is on the same day.
15/06/2012 and 15/06/2012 is on the same day.
15/06/2012 and 25/06/2012 is not on the same day.