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

longgetDateDiff(String startTime, String endTime)
get Date Diff
long diff = 0;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
    Date d1 = df.parse(startTime);
    Date d2 = df.parse(endTime);
    diff = d2.getTime() - d1.getTime();
} catch (Exception e) {
return diff;
longgetDateDiffDay(String begindate, String enddate)
get Date Diff Day
long hourDiff = getDateDiffHour(begindate, enddate);
Float dayDiff = (float) (hourDiff / 24);
return dayDiff.intValue();
doublegetDateDifference(String start, String end)
get Date Difference
double result;
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
String start_fixed = start.substring(0, 23); 
String end_fixed = end.substring(0, 23); 
try {
    Date startDate = format.parse(start_fixed);
    Date endDate = format.parse(end_fixed);
    result = endDate.getTime() - startDate.getTime();
...
intgetDateDifference(String startDateString, String endDateString)
get Date Difference
int returnVal = 0;
if ((startDateString != null) && (endDateString != null)) {
    java.util.Date startDate = getDate(startDateString);
    java.util.Date endDate = getDate(endDateString);
    returnVal = getDateDifference(startDate, endDate);
return returnVal;
longgetDateDiffHour(String begindate, String enddate)
get Date Diff Hour
long minuteDiff = getDateDiffMinute(begindate, enddate);
Float hourDiff = (float) (minuteDiff / 60);
return hourDiff.intValue();
intgetDayDiff(Date firstDate, Date secondDate)
Returns the difference of two dates in days (fristDay - secondDay).
long fristDay = (int) (firstDate.getTime() / (86400. * 1000.));
long secondDay = (int) (secondDate.getTime() / (86400. * 1000.));
return (int) (fristDay - secondDay);
longgetDiff(Date from, Date to)
get Diff
return from.getTime() - to.getTime();
StringgetDiffDate(String srcDate, String format, int diff)
get Diff Date
DateFormat f = new SimpleDateFormat(format);
try {
    Date source = f.parse(srcDate);
    Calendar c = Calendar.getInstance();
    c.setTime(source);
    c.add(Calendar.DATE, diff);
    return f.format(c.getTime());
} catch (Exception e) {
...
longgetDiffDays(Date from, Date to)
get Diff Days
return (to.getTime() - from.getTime()) / 86400000;
intgetDiffDays2(Date one, Date two)
get Diff Days
Calendar sysDate = new GregorianCalendar();
sysDate.setTime(one);
Calendar failDate = new GregorianCalendar();
failDate.setTime(two);
return (int) ((sysDate.getTimeInMillis() - failDate.getTimeInMillis()) / (24 * 60 * 60 * 1000));