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

intdifferenceOnMonth(Date date1, Date date2)
difference On Month
if (date1 == null || date2 == null)
    return 0;
int difYear = 0;
int difMonth = 0;
int year1 = getDateYear(date1);
int year2 = getDateYear(date2);
int month1 = getMonthOnDate(date1);
int month2 = getMonthOnDate(date2);
...
intdifferenceOnYear(Date date1, Date date2)
difference On Year
if (date1 == null || date2 == null) {
    return 0;
int year1 = getDateYear(date1);
int year2 = getDateYear(date2);
return Math.abs(year1 - year2);
longdiffIntoMilliSecond(java.util.Date date, java.util.Date date1)
diff Into Milli Second
if (date == null || date1 == null)
    return 0;
Calendar cal = Calendar.getInstance();
cal.setLenient(true);
cal.setTime(date);
long datet = cal.getTimeInMillis();
cal.setTime(date1);
long date1t = cal.getTimeInMillis();
...
longdiffMillis(Date d1, Date d2)
diff Millis
return d1.getTime() - d2.getTime();
intdiffMonth(Date before, Date after)
diff Month
int monthAll = 0;
int yearsX = diffYear(before, after);
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(before);
c2.setTime(after);
int monthsX = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
monthAll = yearsX * 12 + monthsX;
...
intdiffMonth(Date start, Date end)
diff Month
if (start.after(end)) {
    Date t = start;
    start = end;
    end = t;
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(start);
Calendar endCalendar = Calendar.getInstance();
...
booleandiffOfDate(String begin, String end)
diff Of Date
try {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    Date thisdate = formatter.parse(formatter.format(new Date()));
    Date beginDate = formatter.parse(begin);
    Date endDate = formatter.parse(end);
    if ((thisdate.equals(beginDate) || thisdate.after(beginDate))
            && (thisdate.equals(endDate) || thisdate.before(endDate))) {
        return true;
...
booleandiffY(Date endDate, Date startDate)
diff Y
String endTime = toStringFormat_2(endDate);
String startTime = toStringFormat_2(startDate);
return endTime.compareTo(startTime) >= 1 ? true : false;
longgetDateDiff(String startDate, String endDate)
get Date Diff
long diff = 0;
try {
    Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
    Date date2 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
    diff = (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000) > 0
            ? (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000)
            : (date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000);
} catch (ParseException e) {
...
longgetDateDiff(String startDt, String endDt)
get Date Diff
return getDateDiff(startDt, endDt, "yyyy-MM-dd");