Java Utililty Methods Calendar Compare

List of utility methods to do Calendar Compare

Description

The list of methods to do Calendar Compare are organized into topic(s).

Method

booleanisEqual(Calendar calendar, Date date)
Returns true if the month day and year for the calendar and date object are the same.
if (calendar == null && date == null) {
    return true;
if (calendar == null || date == null) {
    return false;
Calendar calendarValueForDateVariable = Calendar.getInstance();
calendarValueForDateVariable.setTime(date);
...
booleanisEquals(Calendar sourceDate, Calendar compareDate)
is Equals
boolean check = false;
if ((sourceDate.get(Calendar.DATE) == compareDate.get(Calendar.DATE))
        && (sourceDate.get(Calendar.MONTH) == compareDate.get(Calendar.MONTH))
        && (sourceDate.get(Calendar.YEAR) == compareDate.get(Calendar.YEAR))) {
    check = true;
return check;
booleanisSameDate(Calendar d1, Calendar d2)
Returns whether the input dates are on the same date.
return d1.get(Calendar.YEAR) == d2.get(Calendar.YEAR)
        && d1.get(Calendar.DAY_OF_YEAR) == d2.get(Calendar.DAY_OF_YEAR);
booleanisSameDate(java.util.Calendar date1, java.util.Calendar date2)
is Same Date
return (date1.get(java.util.Calendar.DATE) == date2.get(java.util.Calendar.DATE)
        && date1.get(java.util.Calendar.MONTH) == date2.get(java.util.Calendar.MONTH)
        && date1.get(java.util.Calendar.YEAR) == date2.get(java.util.Calendar.YEAR));
booleanisSameInstant(Calendar cal1, Calendar cal2)

Checks if two calendar objects represent the same instant in time.

if (cal1 == null || cal2 == null) {
    throw new IllegalArgumentException("The date must not be null");
return cal1.getTime().getTime() == cal2.getTime().getTime();
booleanisSameInstant(Calendar cal1, Calendar cal2)
is Same Instant
if (cal1 == null || cal2 == null) {
    throw new IllegalArgumentException("The date must not be null");
return cal1.getTime().getTime() == cal2.getTime().getTime();
booleanisSameInstant(Calendar cal1, Calendar cal2)

Checks if two calendar objects represent the same instant in time.

This method compares the long millisecond time of the two objects.

if (cal1 == null || cal2 == null) {
    throw new IllegalArgumentException("The date must not be null");
return cal1.getTime().getTime() == cal2.getTime().getTime();
booleanisSameInstant(final Calendar cal1, final Calendar cal2)
is Same Instant
return cal1.getTime().getTime() == cal2.getTime().getTime();
Calendarmax(Calendar cal1, Calendar cal2)
max
return cal1.after(cal2) ? cal1 : cal2;
Calendarmin(Calendar c1, Calendar c2)
min
Calendar result = Calendar.getInstance();
result.setTimeInMillis(c2.getTimeInMillis());
if (yearsDiff(c1, c2) > 1.0) {
    result.set(WEEK_OF_YEAR, 0);
    result.set(MONTH, 0);
    result.set(DAY_OF_MONTH, 0);
    result.set(HOUR_OF_DAY, 0);
    result.set(MINUTE, 0);
...