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

intcompare(Calendar c1, Calendar c2)
Compare to calendars.
long m1 = c1.getTime().getTime();
long m2 = c2.getTime().getTime();
if (m1 < m2) {
    return -1;
if (m1 > m2) {
    return 1;
return 0;
intcompare(Calendar c1, Calendar c2)
compare
if (c1 == null || c2 == null)
    return -1;
long r = c1.getTimeInMillis() - c2.getTimeInMillis();
if (r > 0)
    return 1;
else if (r == 0)
    return 0;
else
...
intcompare(Calendar c1, Calendar c2, int what)
compare
int number = 0;
switch (what) {
case Calendar.YEAR:
    number = c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
    break;
case Calendar.MONTH:
    int years = compare(c1, c2, Calendar.YEAR);
    number = 12 * years + c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
...
intcompareCalendar(Calendar cal1, Calendar cal2)
Compare two calendars without time comparison
if (cal1 == null || cal2 == null) {
    return 0;
if (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)) {
    return DATE1_EQUAL_DATE2;
if (cal1.get(Calendar.ERA) < cal2.get(Calendar.ERA) || cal1.get(Calendar.YEAR) < cal2.get(Calendar.YEAR)
...
intcompareDates(Calendar date1, Calendar date2)
Method that compares 2 dates, works similarly to the compareTo method from Calendar, but ignores the time, just uses the year, month and day
int result = 0;
if (date1.get(Calendar.YEAR) == date2.get(Calendar.YEAR)) {
    if (date1.get(Calendar.MONTH) == date2.get(Calendar.MONTH)) {
        if (date1.get(Calendar.DAY_OF_MONTH) == date2.get(Calendar.DAY_OF_MONTH)) {
            result = 0;
        } else if (date1.get(Calendar.DAY_OF_MONTH) < date2.get(Calendar.DAY_OF_MONTH)) {
            result = -1;
        } else if (date1.get(Calendar.DAY_OF_MONTH) > date2.get(Calendar.DAY_OF_MONTH)) {
...
intcompareDates(final Calendar firstCal, final Calendar secondCal)
compare Dates
int result = 0;
if (firstCal.get(Calendar.YEAR) > secondCal.get(Calendar.YEAR))
    result = 1;
else if (firstCal.get(Calendar.YEAR) < secondCal.get(Calendar.YEAR))
    result = -1;
else {
    if (firstCal.get(Calendar.MONTH) > secondCal.get(Calendar.MONTH))
        result = 1;
...
intcompareDateTime(final Calendar firstCal, final Calendar secondCal)
compare Date Time
int result = 0;
result = compareDates(firstCal, secondCal);
if (result == 0) {
    result = compareTime(firstCal, secondCal);
if (result == 1)
    System.out.println("first date is greater than the second");
else if (result == -1)
...
intcompareDay(Calendar calendar, Calendar calendar1)
compare Day
int i = compareYear(calendar, calendar1);
if (i != 0)
    return i;
else
    return calendar.get(Calendar.DAY_OF_YEAR) - calendar1.get(Calendar.DAY_OF_YEAR);
intcompareSameDay(Calendar first, Calendar second)
compare Same Day
return (first.get(Calendar.YEAR) - second.get(Calendar.YEAR)) * 365
        + (first.get(Calendar.MONTH) - second.get(Calendar.MONTH)) * 30
        + (first.get(Calendar.DATE) - second.get(Calendar.DATE));
intcompareTime(final Calendar firstCal, final Calendar secondCal)
compare Time
int result = 0;
        System.out.println("Calendar.AM_PM = 0, so comparing HOUR_OF_DAY");
        System.out.println("firstCal HOUR_OF_DAY " + firstCal.get(Calendar.HOUR_OF_DAY));
        System.out.println("secondCal HOUR_OF_DAY " + secondCal.get(Calendar.HOUR_OF_DAY));
        if (firstCal.get(Calendar.HOUR_OF_DAY) > secondCal.get(Calendar.HOUR_OF_DAY))
            result = 1;
...