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

booleanCompareDate(Date date, Date otherdate)
Compare Date
DateFormat format = DateFormat.getDateInstance(DateFormat.MEDIUM);
String datesb1 = format.format(date);
String datesb2 = format.format(otherdate);
return datesb1.equals(datesb2);
intcompareDate(Date date1)
compare Date
Calendar c = Calendar.getInstance();
c.clear();
Date date2 = c.getTime();
long quot;
quot = date1.getTime() - date2.getTime();
quot = quot / 1000 / 60 / 60 / 24;
int day = new Long(quot).intValue();
return day;
...
intcompareDate(Date date1, Date date2)
compare Date
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar1.setTime(date1);
calendar2.setTime(date2);
calendar1.set(Calendar.SECOND, 0);
calendar2.set(Calendar.SECOND, 0);
calendar1.set(Calendar.MILLISECOND, 0);
calendar2.set(Calendar.MILLISECOND, 0);
...
intcompareDate(Date DATE1, Date DATE2)
compare Date
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
    Date dt1 = df.parse(df.format(DATE1));
    Date dt2 = df.parse(df.format(DATE2));
    if (dt1.getTime() > dt2.getTime()) {
        return 1;
    } else if (dt1.getTime() < dt2.getTime()) {
        return -1;
...
StringcompareDate(Date date1, Date date2)
compare Date
if (date1 == null || date2 == null)
    return null;
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(date1);
long d1 = c.getTimeInMillis();
c.setTime(date2);
long d2 = c.getTimeInMillis();
long tmp = (d1 > d2) ? (d1 - d2) : (d2 - d1);
...
intcompareDate(Date date1, Date date2)
compare Date
long due = date1.getTime() + 0 * 24 * 60 * 60 * 1000;
long due2 = date2.getTime() + 0 * 24 * 60 * 60 * 1000;
if (due == due2) {
    return 0;
} else if (due > due2) {
    return 1;
} else
    return -1;
...
intcompareDate(Date date1, Date date2)
compare Date
if (date1 == null || date2 == null)
    return -1;
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
return cal1.compareTo(cal2);
intcompareDate(Date date1, Date date2)
compare Date
Calendar c1 = Calendar.getInstance();
c1.setTime(date1);
Calendar c2 = Calendar.getInstance();
c2.setTime(date2);
if (c1.get(Calendar.YEAR) != c2.get(Calendar.YEAR))
    return c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
if (c1.get(Calendar.MONTH) != c2.get(Calendar.MONTH))
    return c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
...
intcompareDate(Date date1, Date date2)
compare Date
Calendar c1 = Calendar.getInstance();
c1.setTime(date1);
Calendar c2 = Calendar.getInstance();
c2.setTime(date2);
return compareDate(c1, c2);
intcompareDate(Date date1, Date date2, String pattern)
compare Date
String d1 = date2Str(date1, pattern);
String d2 = date2Str(date2, pattern);
return d1.compareTo(d2);