Check two Calendar Instance Has Same Day In Java

On many occasions we need to check if two calendar objects or Date are on the same day ignoring time while writing programs in Java. 29 Apr 2002 13:40 and 29 Apr 2002 08:01 would return true while 29 Apr 2002 13:40 and 19 Apr 2002 08:01 would return false. This can be done using following functions.

public static boolean isSameDay(Calendar cal1, Calendar cal2) {
if (cal1 == null || cal2 == null) {
throw new IllegalArgumentException("The date must not be null");
}
return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA)
&& cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1
.get(Calendar.DAY_OF_YEAR) == cal2
.get(Calendar.DAY_OF_YEAR));
}
public static boolean isSameDay(Date date1, Date date2) {
if (date1 == null || date2 == null) {
throw new IllegalArgumentException("The date must not be null");
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
return isSameDay(cal1, cal2);
}
public static boolean isSameDay(Date date1, Calendar cal1) {
if (date1 == null || cal1 == null) {
throw new IllegalArgumentException("The date must not be null");
}
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date1);
return isSameDay(cal1, cal2);
}

Using above functions you can check day equality between two Calendar objects, two Date objects or One Calendar object and one two Date object