Java Utililty Methods Date Compare

List of utility methods to do Date Compare

Description

The list of methods to do Date Compare are organized into topic(s).

Method

intcompareDays(final Date date1, final Date date2)
Compare two dates considering "day" as the most significant field.
Calendar calendar1 = getCalendarInstance(date1);
Calendar calendar2 = getCalendarInstance(date2);
for (int field : DATE_FIELDS) {
    int fieldValue1 = calendar1.get(field);
    int fieldValue2 = calendar2.get(field);
    if (fieldValue1 != fieldValue2) {
        return fieldValue1 - fieldValue2;
return 0;
booleancompareFileDates(final Date date1, final Date date2)
For some reason modification milliseconds for some files are unstable, use this function to compare file dates ignoring milliseconds.
if ((date1 == null) || (date2 == null)) {
    return false;
Calendar cldr = Calendar.getInstance(Locale.ENGLISH);
cldr.setTime(date1);
cldr.set(Calendar.MILLISECOND, 0);
long dt1 = cldr.getTimeInMillis();
cldr.setTime(date2);
...
intcompareMonth(Date first, Date second)
compare Month
Calendar cf = Calendar.getInstance();
cf.setTime(first);
Calendar cs = Calendar.getInstance();
cs.setTime(second);
int offset = (cs.get(Calendar.YEAR) - cf.get(Calendar.YEAR)) * 12
        + (cs.get(Calendar.MONTH) - cf.get(Calendar.MONTH));
return offset;
intcompareSecond(java.util.Date date1, java.util.Date date2)
compare Second
if (date1 == null || date2 == null)
    return 0;
long time1 = date1.getTime();
long time2 = date2.getTime();
long margin = time1 - time2;
Long longValue = new Long(margin / (1000));
return longValue.intValue();
booleancompareStartDateEndDate(String startDate, String endDate)
Compares two dates given as Strings.
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT_AND_TIME);
Date sDate = null;
Date eDate = null;
try {
    sDate = formatter.parse(startDate);
    eDate = formatter.parse(endDate);
} catch (ParseException e) {
    e.printStackTrace();
...
intcompareTime(Date date1, Date date2)
compare Time
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
int date1Hour = cal.get(Calendar.HOUR_OF_DAY);
int date1Minute = cal.get(Calendar.MINUTE);
cal.setTime(date2);
int date2Hour = cal.get(Calendar.HOUR_OF_DAY);
int date2Minute = cal.get(Calendar.MINUTE);
if (date1Hour == date2Hour && date1Minute == date2Minute) {
...
intcompareTime(Date first, Date second)
compare Time
Calendar firstCal = new GregorianCalendar();
firstCal.setTime(first);
firstCal.set(0, 0, 0);
Calendar secondCal = new GregorianCalendar();
secondCal.setTime(second);
secondCal.set(0, 0, 0);
return firstCal.compareTo(secondCal);
longcompareTimesOfDay(Date time1, Date time2)
Calculates difference in milliseconds taking into account only time of day.
Calendar cal1 = Calendar.getInstance();
cal1.setTime(time1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(time2);
long difference = 0;
difference = (cal1.get(Calendar.HOUR_OF_DAY) - cal2.get(Calendar.HOUR_OF_DAY)) * 60 * 60 * 1000;
difference += (cal1.get(Calendar.MINUTE) - cal2.get(Calendar.MINUTE)) * 60 * 1000;
difference += (cal1.get(Calendar.SECOND) - cal2.get(Calendar.SECOND)) * 1000;
...
intcompareTo(Date src, Date dest)
compare To
Date d1 = parseDateTime(formatDateTime(src, DATETIME_PATTERN), DATETIME_PATTERN);
Date d2 = parseDateTime(formatDateTime(dest, DATETIME_PATTERN), DATETIME_PATTERN);
int i = d1.compareTo(d2);
return i;
booleancompareToCal(Date sDate, Date eDate)
compare To Cal
Calendar sCal = new GregorianCalendar();
sCal.setTime(sDate);
Calendar eCal = new GregorianCalendar();
eCal.setTime(eDate);
return (sCal.getTimeInMillis() == eCal.getTimeInMillis() || before(sDate, eDate));