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

longdateDiff(Date beginDate, Date endDate)
date Diff
long beginDateseconds = beginDate.getTime();
long endDateseconds = endDate.getTime();
long diff = endDateseconds - beginDateseconds;
long diffDays = diff / (24 * 60 * 60 * 1000);
return diffDays;
longdateDiff(Date d1, Date d2, int field)
date Diff
long j = 1;
switch (field) {
case Calendar.YEAR:
    return dateDiffYear(d1, d2);
case Calendar.MONTH:
    return dateDiffMonth(d1, d2);
case Calendar.DATE:
    j = 1000 * 60 * 60 * 24;
...
StringdateDiff(Date date1, Date date2)
date Diff
long milliseconds1 = date1.getTime();
long milliseconds2 = date2.getTime();
long diff = milliseconds2 - milliseconds1;
long diffSeconds = diff / 1000;
long diffMinutes = diff / (60 * 1000);
long diffHours = diff / (60 * 60 * 1000);
long diffDays = diff / (24 * 60 * 60 * 1000);
diffSeconds = diffSeconds - (diffMinutes * 60);
...
intdateDiff(Date date1, Date date2)
Returns the difference in days between date1 and date2
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
int diff = 0;
while (cal1.before(cal2)) {
    cal1.add(Calendar.DATE, 1);
    diff++;
...
intdateDiff(Date date1, Date date2)
date Diff
int count = 0;
int diff = 0;
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
while (cal1.get(Calendar.YEAR) != cal2.get(Calendar.YEAR)) {
    count = 365 * (cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR));
...
longdateDiff(Date fromDate, Date toDate)
date Diff
return dateDiff(getDateTime(DATE_PATTERN, fromDate), getDateTime(DATE_PATTERN, toDate));
longDateDiff(Date startDate, Date endDate)
Date Diff
long totalDate = 0;
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
long timestart = calendar.getTimeInMillis();
calendar.setTime(endDate);
long timeend = calendar.getTimeInMillis();
totalDate = (timeend - timestart) / (1000 * 60 * 60 * 24);
return totalDate;
...
longdateDiff(final Date date1, final Date date2)
Calculate difference in milliseconds from 2 dates.
long result = date1.getTime() - date2.getTime();
return result;
intdateDiff(int category, Date date1, Date date2)
date Diff
Calendar c1 = Calendar.getInstance();
c1.setTime(date1);
Calendar c2 = Calendar.getInstance();
c2.setTime(date2);
if (YEAR == category) {
    return Math.abs(c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR));
} else if (MONTH == category) {
    return Math.abs((((c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR)) * 12) + c2.get(Calendar.MONTH))
...
intdateDiff(int interval, Date begin, Date end)
date Diff
long beginTime = begin.getTime() / 1000L;
long endTime = end.getTime() / 1000L;
long tmp = 0L;
if (endTime == beginTime) {
    return 0;
if (endTime < beginTime) {
    tmp = beginTime;
...