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

longdiffDays(Calendar startDate, Calendar endDate)
Returns the number of days between the specified start date and end date.
Calendar sDate = (Calendar) startDate.clone();
long daysBetween = 0;
int y1 = sDate.get(Calendar.YEAR);
int y2 = endDate.get(Calendar.YEAR);
int m1 = sDate.get(Calendar.MONTH);
int m2 = endDate.get(Calendar.MONTH);
while (((y2 - y1) * 12 + (m2 - m1)) > 12) {
    if (sDate.get(Calendar.MONTH) == Calendar.JANUARY
...
longdifferenceInDays(Calendar date1, Calendar date2)
difference In Days
final long msPerDay = 1000 * 60 * 60 * 24;
final long date1Milliseconds = date1.getTime().getTime();
final long date2Milliseconds = date2.getTime().getTime();
final long result = (date1Milliseconds - date2Milliseconds) / msPerDay;
return result;
StringformatTimeDiff(Calendar ref, Calendar now, boolean ignoreMS)
format Time Diff
if (ref != null && now != null) {
    long diff = now.getTimeInMillis() - ref.getTimeInMillis();
    StringBuffer sb = new StringBuffer();
    if (diff < 0) {
        sb.append('-');
        diff = -diff;
    } else {
        sb.append('+');
...
intgetDateDiff(Calendar c1, Calendar c2)
get Date Diff
return (int) ((c2.getTimeInMillis() - c1.getTimeInMillis()) / (1000 * 60 * 60 * 24));
longgetDateDiff(Calendar d1, Calendar d2)
Computes the number of minutes between two Dates.
if ((d1 == null) || (d2 == null)) {
    throw new NullPointerException("d1 or d2 null");
long diff = d1.getTime().getTime() - d2.getTime().getTime();
long res = diff / (1000 * 60);
return res;
floatgetDateDiff(Date d1, Date d2, int gregorianCalendarUnits)
get Date Diff
long millis = d1.getTime() - d2.getTime();
switch (gregorianCalendarUnits) {
case Calendar.MILLISECOND:
    return millis;
case Calendar.SECOND:
    return millis / 1000;
case Calendar.MINUTE:
    return millis / 1000 / 60;
...
intgetDaysDifference(Calendar refDate, Date date)
get Days Difference
long delta = date.getTime() - refDate.getTimeInMillis();
long deltaDays = (delta) / MILLIS_PER_DAY;
return (int) deltaDays;
intgetDifference(int constant, Calendar first, Calendar second)
Get the difference in time between two calendars for an individual time unit.
int difference = 0;
Calendar temp = new GregorianCalendar();
temp.setTimeInMillis(first.getTimeInMillis());
while (!temp.after(second)) {
    temp.add(constant, 1);
    difference += 1;
return difference - 1;
...
intgetDifference(int field, Calendar aCalX, Calendar aCalY)
get Difference
int gap = 0;
int step = 0;
Calendar calX = new GregorianCalendar();
Calendar calY = new GregorianCalendar();
calX.set(aCalX.get(Calendar.YEAR), aCalX.get(Calendar.MONTH), aCalX.get(Calendar.DATE), 0, 0, 0);
calY.set(aCalY.get(Calendar.YEAR), aCalY.get(Calendar.MONTH), aCalY.get(Calendar.DATE), 0, 0, 0);
step = (calX.before(calY) ? -1 : 1);
if (field == Calendar.DATE) {
...
intgetDifferenceInDays(Calendar fromDate, Calendar toDate)
get Difference In Days
int numberOfDays = 0;
long delta;
delta = toDate.getTimeInMillis() - fromDate.getTimeInMillis();
numberOfDays = (int) ((((delta / 1000) / 60) / 60) / 24);
return (numberOfDays);