Java Utililty Methods Calendar Different

List of utility methods to do Calendar Different

Description

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

Method

longgetDifferenceInSeconds(Calendar calendarStart, Calendar calendarEnd)
Returns the difference in seconds between two Calendar objects.
return (calendarEnd.getTimeInMillis() - calendarStart.getTimeInMillis()) / 1000;
intgetDiffPartialYears(Date date, GregorianCalendar b)
get Diff Partial Years
GregorianCalendar a = new GregorianCalendar();
a.setTime(date);
int diff = b.get(GregorianCalendar.YEAR) - a.get(GregorianCalendar.YEAR);
if (diff == 0 && countDaysBetween(a.getTime(), b.getTime()) > 0) {
    return 1;
return diff;
DategetDiffTargetDate(Calendar cal, int diffDay)
get Diff Target Date
if (null == cal) {
    cal = Calendar.getInstance();
cal.add(Calendar.DATE, diffDay);
return cal.getTime();
floatgetMillisDiff(long lhs, long rhs, int gregorianCalendarUnits)
get Millis Diff
long millis = lhs - rhs;
switch (gregorianCalendarUnits) {
case Calendar.MILLISECOND:
    return millis;
case Calendar.SECOND:
    return (float) (millis / SECOND);
case Calendar.MINUTE:
    return (float) (millis / MINUTE);
...
intgetMonthDiff(Calendar c1, Calendar c2)
get Month Diff
int mon1 = c1.get(Calendar.MONTH);
int mon2 = c2.get(Calendar.MONTH);
int y1 = c1.get(Calendar.YEAR);
int y2 = c2.get(Calendar.YEAR);
return (y2 - y1) * 12 + mon2 - mon1;
intgetMonthsDifference(Calendar date1, Calendar date2)
get Months Difference
int m1 = date1.get(Calendar.YEAR) * 12 + date1.get(Calendar.MONTH);
int m2 = date2.get(Calendar.YEAR) * 12 + date2.get(Calendar.MONTH);
return m2 - m1;
DategetTargetDate(Calendar reqCal, int diffMon, int proxyDay)
get Target Date
Calendar cal = null == reqCal ? Calendar.getInstance() : reqCal;
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + diffMon);
cal.set(Calendar.DATE, proxyDay);
Date date = cal.getTime();
return date;
longgetTimeDifference(String unit, Calendar start, Calendar end)
get Time Difference
long diff = end.getTimeInMillis() - start.getTimeInMillis();
switch (functions.get(unit)) {
case 20: 
    diff = diff / ONE_MINUTE;
    break;
case 21: 
    diff = diff / ONE_HOUR;
    break;
...
doublegetTimeDiffSecond(Calendar first, Calendar second)
get Time Diff Second
if (first == null || second == null)
    return 0;
final double millis = second.getTimeInMillis() - first.getTimeInMillis();
return Math.abs(millis / 1000);
intgetWeekDifference(Calendar before, Calendar after)
get Week Difference
int diff = after.get(Calendar.WEEK_OF_YEAR) - before.get(Calendar.WEEK_OF_YEAR);
diff = diff + (before.getMaximum(Calendar.WEEK_OF_YEAR)
        * (after.get(Calendar.YEAR) - before.get(Calendar.YEAR)));
return diff;