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

intgetDifference(Date a, Date b)
get Difference
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 {
...
longgetDifference(String dateStr1, String dateStr2, char choice)
Gets the difference between 2 dates in Seconds or Minutes or Hours or Days based on the choice given.
if (dateStr1 == null || dateStr2 == null)
    throw new RuntimeException("Invalid Input");
DateFormat df = new SimpleDateFormat(STANDARD_DATE_FORMAT_STRING);
Date date1 = null, date2 = null;
try {
    date1 = df.parse(dateStr1);
} catch (ParseException e) {
    e.printStackTrace();
...
longgetDifferenceInDays(Calendar calendar0, Calendar calendar1)
get Difference In Days
return getDifferenceInDays(calendar0.getTimeInMillis(), calendar1.getTimeInMillis());
StringgetDiffMon(Date dt, int idiff)
get Diff Mon
Calendar c = Calendar.getInstance();
c.setTime(dt);
c.add(Calendar.MONTH, idiff);
return dtSimpleFormat(c.getTime());
StringgetDiffMon(Date dt, int idiff)
get Diff Mon
Calendar c = Calendar.getInstance();
c.setTime(dt);
c.add(Calendar.MONTH, idiff);
return dateFormat(c.getTime());
StringgetDiffToString(Date date, int n, String pattern)
get Diff To String
Date returnDay = new Date(date.getTime() + n * 24 * 3600 * 1000L);
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.format(returnDay);
StringgetScoreDifferenceString(int newNumber, int oldNumber)
get Score Difference String
DecimalFormat format = new DecimalFormat("+#;-#");
return format.format(newNumber - oldNumber);
longgetSecondDiff(Date d1, Date d2)
get Second Diff
long d1t = d1.getTime();
long d2t = d2.getTime();
return (d2t - d1t) / 1000;
doublegetTimeDifferenceInSeconds(Date d, Date d2)
get Time Difference In Seconds
return Math.abs(d.getTime() - d2.getTime()) / 1000.0;
DatenextSeconds(Date date, int diff)
next Seconds
return add(date, Calendar.SECOND, diff);