List of utility methods to do Calendar Different
long | getDifferenceInSeconds(Calendar calendarStart, Calendar calendarEnd) Returns the difference in seconds between two Calendar objects. return (calendarEnd.getTimeInMillis() - calendarStart.getTimeInMillis()) / 1000;
|
int | getDiffPartialYears(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; |
Date | getDiffTargetDate(Calendar cal, int diffDay) get Diff Target Date if (null == cal) { cal = Calendar.getInstance(); cal.add(Calendar.DATE, diffDay); return cal.getTime(); |
float | getMillisDiff(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); ... |
int | getMonthDiff(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; |
int | getMonthsDifference(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; |
Date | getTargetDate(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;
|
long | getTimeDifference(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; ... |
double | getTimeDiffSecond(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); |
int | getWeekDifference(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; |