Java Utililty Methods Date Difference

List of utility methods to do Date Difference

Description

The list of methods to do Date Difference are organized into topic(s).

Method

longdayDiff(Date date1, Date date2)
day Diff
return (date2.getTime() - date1.getTime()) / 86400000;
intdayDiff(Date first, Date second)
day Diff
long msPerDay = 1000 * 60 * 60 * 24;
long diff = (first.getTime() / msPerDay) - (second.getTime() / msPerDay);
Long convertLong = new Long(diff);
return convertLong.intValue();
longdayDiffByStartOfDay(Date a, Date b)
day Diff By Start Of Day
a = startOfDay(a);
b = startOfDay(b);
return dayDiff(a, b);
longdaysDiff(Date d1, Date d2)
days Diff
long t1 = d1.getTime();
long t2 = d2.getTime();
return (t1 - t2) / (1000 * 60 * 60 * 24);
longdiff(Date d1, Date d2)
Return the millisecond difference between two Dates.
return d1.getTime() - d2.getTime();
intdiff(Date date1, Date date2)
diff
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(date1);
c2.setTime(date2);
int diffDay = 0;
if (c1.before(c2)) {
    diffDay = countDiffDay(c1, c2);
} else {
...
intdiff(Date date1, Date date2)
diff
long day = 24L * 60L * 60L * 1000L;
String str1 = date2Str(date1, "yyyy-MM-dd");
date1 = str2Date(str1, "yyyy-MM-dd");
String str2 = date2Str(date2, "yyyy-MM-dd");
date2 = str2Date(str2, "yyyy-MM-dd");
return (int) (((date2.getTime() - date1.getTime()) / day));
longdiff(Date endDate, Date startDate)
diff
long endTime = getMillis(endDate);
long startTime = getMillis(startDate);
return endTime - startTime;
longdiff(Date sDate, Date fDate)
diff
long timeDiff = fDate.getTime() - sDate.getTime();
return timeDiff;
longdiff(Date subtrahend, Date minuend, long diffField)
diff
long diff = minuend.getTime() - subtrahend.getTime();
return diff / diffField;