Java Calendar Compare compareCalendar(Calendar cal1, Calendar cal2)

Here you can find the source of compareCalendar(Calendar cal1, Calendar cal2)

Description

Compare two calendars without time comparison

License

Open Source License

Parameter

Parameter Description
cal1 first calendar
cal2 second calendar

Return

DATE1_EQUAL_DATE2 if the dates are the same, DATE1_BEFORE_DATE2 if the date1 is before date2 and DATE1_AFTER_DATE2 otherwise

Declaration

public static int compareCalendar(Calendar cal1, Calendar cal2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;

public class Main {
    public final static int DATE1_BEFORE_DATE2 = -1;
    public final static int DATE1_AFTER_DATE2 = 1;
    public final static int DATE1_EQUAL_DATE2 = 0;

    /**/*from  www .  j a v a2s  .  c  o m*/
     * Compare two calendars without time comparison
     * @param cal1 first calendar
     * @param cal2 second calendar
     * @return DATE1_EQUAL_DATE2 if the dates are the same, DATE1_BEFORE_DATE2 if the date1 is before date2 and DATE1_AFTER_DATE2 otherwise
     */
    public static int compareCalendar(Calendar cal1, Calendar cal2) {
        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)
                || cal1.get(Calendar.DAY_OF_YEAR) < cal2.get(Calendar.DAY_OF_YEAR)) {
            return DATE1_BEFORE_DATE2;
        }

        return DATE1_AFTER_DATE2;
    }
}

Related

  1. compare(Calendar c1, Calendar c2)
  2. compare(Calendar c1, Calendar c2)
  3. compare(Calendar c1, Calendar c2, int what)
  4. compareDates(Calendar date1, Calendar date2)
  5. compareDates(final Calendar firstCal, final Calendar secondCal)
  6. compareDateTime(final Calendar firstCal, final Calendar secondCal)
  7. compareDay(Calendar calendar, Calendar calendar1)