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

intcountDiffDay(Calendar c1, Calendar c2)
count Diff Day
int returnInt = 0;
while (!c1.after(c2)) {
    c1.add(Calendar.DAY_OF_MONTH, 1);
    returnInt++;
if (returnInt > 0) {
    returnInt = returnInt - 1;
return (returnInt);
intcountDiffMonth(Calendar cBegin, Calendar cEnd, boolean isRoundUp)
count Diff Month
int diffMonth = 0;
int dBegin = 0;
int dEnd = 0;
int mBegin = 0;
int mEnd = 0;
try {
    dBegin = cBegin.get(Calendar.DATE);
    dEnd = cEnd.get(Calendar.DATE);
...
intdatediff(Calendar c1, Calendar c2)
datediff
if (c1 == null || c2 == null)
    return -1;
long r = c1.getTimeInMillis() - c2.getTimeInMillis();
r = r / (24 * 60 * 60 * 1000);
return (int) r;
intdateDiff(int type, Calendar fromDate, Calendar toDate)
Get the difference of a specific calendar field for 2 calendar instances.
boolean future = toDate.after(fromDate);
int diff = 0;
long savedDate = fromDate.getTimeInMillis();
while ((future && !fromDate.after(toDate)) || (!future && !fromDate.before(toDate))) {
    savedDate = fromDate.getTimeInMillis();
    fromDate.add(type, future ? 1 : -1);
    diff++;
diff--;
fromDate.setTimeInMillis(savedDate);
return diff;
intdateDiff(int type, Calendar fromDate, Calendar toDate, boolean future)
date Diff
int diff = 0;
long savedDate = fromDate.getTimeInMillis();
while ((future && !fromDate.after(toDate)) || (!future && !fromDate.before(toDate))) {
    savedDate = fromDate.getTimeInMillis();
    fromDate.add(type, future ? 1 : -1);
    diff++;
diff--;
...
intdateDiff(int type, Calendar fromDate, Calendar toDate, boolean future)
date Diff
int year = Calendar.YEAR;
int fromYear = fromDate.get(year);
int toYear = toDate.get(year);
if (Math.abs(fromYear - toYear) > MAX_YEARS) {
    toDate.set(year, fromYear + (future ? MAX_YEARS : -MAX_YEARS));
int diff = 0;
long savedDate = fromDate.getTimeInMillis();
...
intdateDiff(long dateUtilitiesUnitField, Calendar firstDate, Calendar secondDate)
Calculate the difference, in DateUtilitities field units, for any two Calendar objects
long diff = Math.abs(firstDate.getTimeInMillis() - secondDate.getTimeInMillis());
double diffAmt = (double) diff / dateUtilitiesUnitField;
return (int) Math.round(diffAmt);
StringdateDifferenceInMin(Calendar startDate, Calendar stopDate)
This method return the min as string between two dates.
return "" + ((stopDate.getTimeInMillis() - startDate.getTimeInMillis())) / (1000 * 60);
longdiff(Calendar value1, Calendar value2, int millisInCondition)
diff
long result = 0;
long v1 = (value1.getTimeInMillis() + value1.get(Calendar.ZONE_OFFSET)) / millisInCondition;
long v2 = (value2.getTimeInMillis() + value2.get(Calendar.ZONE_OFFSET)) / millisInCondition;
result = v2 - v1;
return result;
CalendardiffDay(Calendar calendar, int day)
diff Day
return diff(calendar, 0, 0, day, 0, 0, 0, 0);