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

intdiffDay(Date date1, Date date2)
diff Day
Calendar aCalendar = Calendar.getInstance();
aCalendar.setTime(date1);
int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);
Calendar bCalendar = Calendar.getInstance();
bCalendar.setTime(date2);
int day2 = bCalendar.get(Calendar.DAY_OF_YEAR);
return day2 - day1;
longdiffDay(Date start, Date end)
diff Day
return offset(start, end, Calendar.DAY_OF_YEAR);
intdiffDay(Date startDate, Date endDate)
Returns the number of days difference between two dates DUPLICATE METHOD?
if (startDate == null || endDate == null) {
    return 0;
return (int) ((endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24));
intdifferDays(Date src, Date target)
differ Days
Calendar cal = getDateCalendar(src);
Calendar cal2 = getDateCalendar(target);
return new Long((cal2.getTimeInMillis() - cal.getTimeInMillis()) / (1000 * 60 * 60 * 24)).intValue();
longdifference(Date date1, Date date2)
difference
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
if (cal2.after(cal1)) {
    return cal2.getTimeInMillis() - cal1.getTimeInMillis();
return cal1.getTimeInMillis() - cal2.getTimeInMillis();
...
intdifferenceDay(Date toDate, Date fromDate)
difference Day
Calendar to = Calendar.getInstance();
to.setTime(toDate);
Calendar from = Calendar.getInstance();
from.setTime(fromDate);
long day = (to.getTimeInMillis() - from.getTimeInMillis()) / 1000 / 60 / 60 / 24;
return (int) day;
doubleDifferenceInMilliseconds(Date date1, Date date2)
Difference In Milliseconds
return GetTimeInMilliseconds(date1) - GetTimeInMilliseconds(date2);
intdifferenceInMonths(Date beginningDate, Date endingDate)
calculate difference in months between the arguments.
if (beginningDate == null || endingDate == null) {
    return 0;
Calendar cal1 = new GregorianCalendar();
cal1.setTime(beginningDate);
Calendar cal2 = new GregorianCalendar();
cal2.setTime(endingDate);
return differenceInMonths(cal1, cal2);
...
doubleDifferenceInSeconds(Date date1, Date date2)
Finds the difference in seconds between two dates.
return DifferenceInMilliseconds(date1, date2) / 1000.0;
intdifferenceMonth(Date date1, Date date2)
difference Month
Calendar cal1 = Calendar.getInstance(timeZone);
cal1.setTime(date1);
cal1.set(Calendar.DATE, 1);
Calendar cal2 = Calendar.getInstance(timeZone);
cal2.setTime(date2);
cal2.set(Calendar.DATE, 1);
int count = 0;
if (cal1.before(cal2)) {
...