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

intdateDiff(java.util.Date a, java.util.Date b)
Date diff.
int tempDifference = 0;
int difference = 0;
Calendar earlier = Calendar.getInstance();
Calendar later = Calendar.getInstance();
if (a.compareTo(b) < 0) {
    earlier.setTime(a);
    later.setTime(b);
} else {
...
longdateDiff(String startDate, String endDate)
date Diff
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
GregorianCalendar endGC = new GregorianCalendar();
long times, days1 = 0l;
try {
    times = sd.parse(endDate).getTime() - sd.parse(startDate).getTime();
    long days = times / (1000 * 24 * 60 * 60);
    days1 = (days / 7) * 5;
    long days2 = days % 7;
...
long[]dateDiff(String startTime, String endTime, String format)
date Diff
SimpleDateFormat sd = new SimpleDateFormat(format);
long nd = 86400000L;
long nh = 3600000L;
long nm = 60000L;
long ns = 1000L;
long[] date = new long[4];
try {
    long diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
...
intDateDiff(String strDateBegin, String strDateEnd, int iType)
Date Diff
Calendar calBegin = parseDateTime(strDateBegin);
Calendar calEnd = parseDateTime(strDateEnd);
long lBegin = calBegin.getTimeInMillis();
long lEnd = calEnd.getTimeInMillis();
int ss = (int) ((lBegin - lEnd) / 1000L);
int min = ss / 60;
int hour = min / 60;
int day = hour / 24;
...
longdateDiffer(String time1, String time2, String formatStr)
date Differ
SimpleDateFormat sp = new SimpleDateFormat(formatStr);
Date date1 = sp.parse(time1);
Date date2 = sp.parse(time2);
long differ = Math.abs(date1.getTime() - date2.getTime());
return differ;
floatdateDiffInDays(Date dateStart, Date dateEnd)
computes (dateEnd - dateStart) in days
return ((float) (dateEnd.getTime() - dateStart.getTime())) / ONE_DAY_IN_MILLIS;
floatdateDiffInDaysIgnoreTime(Date dateStart, Date dateEnd)
date Diff In Days Ignore Time
return dateDiffInDays(getMidnight(dateStart), getMidnight(dateEnd));
longdateDiffMonth(Date d1, Date d2)
date Diff Month
Calendar calendar = Calendar.getInstance();
calendar.setTime(d1);
int y1 = calendar.get(Calendar.YEAR);
int m1 = calendar.get(Calendar.MONTH);
calendar.setTime(d2);
int y2 = calendar.get(Calendar.YEAR);
int m2 = calendar.get(Calendar.MONTH);
return (y1 - y2) * 12 + m1 - m2;
...
longdateDiffWithNow(Calendar sDate)
date Diff With Now
Date now = Calendar.getInstance().getTime();
long timeDiff = now.getTime() - sDate.getTimeInMillis();
return timeDiff;
longdayDiff(Date a, Date b)
day Diff
if (a == null || b == null) {
    return 0;
long diff = a.getTime() - b.getTime();
if (diff >= 0) {
    diff = diff + HOUR_IN_MILLIS;
} else {
    diff = diff - HOUR_IN_MILLIS;
...