src/main/java/pw/phylame/util/DateUtils.java
/**
     * Returns interval between tow dates.
     * @param sDate begin date
     * @param eDate end date
     * @param type interval type, maybe "Y/y": year, "M/m": month, "D/d": day
     * @return interval value
     */
Returns interval between tow dates.
Returns interval between tow dates .
public static int calculateInterval(Date sDate, Date eDate, String type) {
        int interval = 0;
        Calendar sCalendar = Calendar.getInstance();
        sCalendar.setTime(sDate);
        int sYears = sCalendar.get(Calendar.YEAR);
        int sMonths = sCalendar.get(Calendar.MONTH);
        int sDays = sCalendar.get(Calendar.DAY_OF_YEAR);
        Calendar eCalendar = Calendar.getInstance();
        eCalendar.setTime(eDate);
        int eYears = eCalendar.get(Calendar.YEAR);
        int eMonths = eCalendar.get(Calendar.MONTH);
        int eDays = eCalendar.get(Calendar.DAY_OF_YEAR);
        if ("Y".equals(type) || "y".equals(type)) {
            interval = eYears - sYears;
            if (eMonths < sMonths) {
                --interval;
            }
        } else if ("M".equals(type) || "m".equals(type)) {
            interval = 12 * (eYears - sYears);
            interval += (eMonths - sMonths);
        } else if ("D".equals(type) || "d".equals(type)) {
            interval = 365 * (eYears - sYears);
            interval += (eDays - sDays);
            while(sYears < eYears) {
                if(isLeapYear(sYears)) {
                    --interval;
                }
                ++sYears;
            }
        }
        return interval;
    }
public static int calculateInterval ( Date sDate , Date eDate , String type ) { int interval = 0 ; Calendar sCalendar = Calendar . getInstance ( ) ; sCalendar . setTime ( sDate ) ; int sYears = sCalendar . get ( Calendar . YEAR ) ; int sMonths = sCalendar . get ( Calendar . MONTH ) ; int sDays = sCalendar . get ( Calendar . DAY_OF_YEAR ) ; Calendar eCalendar = Calendar . getInstance ( ) ; eCalendar . setTime ( eDate ) ; int eYears = eCalendar . get ( Calendar . YEAR ) ; int eMonths = eCalendar . get ( Calendar . MONTH ) ; int eDays = eCalendar . get ( Calendar . DAY_OF_YEAR ) ; if ( "Y" . equals ( type ) || "y" . equals ( type ) ) { interval = eYears - sYears ; if ( eMonths < sMonths ) { -- interval ; } } else if ( "M" . equals ( type ) || "m" . equals ( type ) ) { interval = 12 * ( eYears - sYears ) ; interval += ( eMonths - sMonths ) ; } else if ( "D" . equals ( type ) || "d" . equals ( type ) ) { interval = 365 * ( eYears - sYears ) ; interval += ( eDays - sDays ) ; while ( sYears < eYears ) { if ( isLeapYear ( sYears ) ) { -- interval ; } ++ sYears ; } } return interval ; }
Returns interval between tow dates.
Returns interval between tow dates .
// remove days in leap year