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

booleanafter(String format, String baseDate, String compareDate)
after
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
try {
    Date base = simpleDateFormat.parse(baseDate);
    Date compare = simpleDateFormat.parse(compareDate);
    return compare.after(base);
} catch (ParseException e) {
    throw new RuntimeException(e.getMessage());
Integercompare(Date d1, Date d2)
compare
java.util.Calendar c1 = java.util.Calendar.getInstance();
java.util.Calendar c2 = java.util.Calendar.getInstance();
c1.setTime(d1);
c2.setTime(d2);
return c1.compareTo(c2);
intcompare(Date d1, Date d2)
Compares two dates taking into consideration only the year, month and day
Calendar c1 = Calendar.getInstance();
c1.setTime(d1);
Calendar c2 = Calendar.getInstance();
c2.setTime(d2);
if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) {
    if (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH)) {
        return c1.get(Calendar.DAY_OF_MONTH) - c2.get(Calendar.DAY_OF_MONTH);
    return c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
return c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
Stringcompare(Date d1, Date d2)
compare
Calendar c1 = Calendar.getInstance();
c1.setTime(d1);
Calendar c2 = Calendar.getInstance();
c2.setTime(d2);
if (c1.after(c2)) {
    return ">";
} else if (c1.before(c2)) {
    return "<";
...
Booleancompare(Date date, Date fromDate, Date toDate)
compare
Calendar date1 = Calendar.getInstance();
date1.setTime(date);
Calendar from = Calendar.getInstance();
from.setTime(fromDate);
Calendar to = Calendar.getInstance();
to.setTime(toDate);
if (date1.after(from) && date1.before(to)) {
    return false;
...
intcompare(Date date1, Date date2, String format)
compare
if (date1 == null && date2 == null) {
    return 0;
if (date1 == null) {
    return -1;
if (date2 == null) {
    return 1;
...
intCompare(Date dtA, Date dtB, boolean bUnkIsPast)
Compare
if (bUnkIsPast) {
    boolean bInvA = ((dtA.getTime() >= m_dtInvMin.getTime()) && (dtA.getTime() <= m_dtInvMax.getTime())
            && (dtA.getMinutes() == 59) && (dtA.getSeconds() == 59));
    boolean bInvB = ((dtB.getTime() >= m_dtInvMin.getTime()) && (dtB.getTime() <= m_dtInvMax.getTime())
            && (dtB.getMinutes() == 59) && (dtB.getSeconds() == 59));
    if (bInvA)
        return (bInvB ? 0 : -1);
    if (bInvB)
...
intcompare(Date start, Date end)
compare
Calendar c1 = Calendar.getInstance();
c1.setTime(start);
Calendar c2 = Calendar.getInstance();
c2.setTime(end);
int year1 = c1.get(Calendar.YEAR);
int year2 = c2.get(Calendar.YEAR);
if (year1 != year2) {
    return year1 - year2;
...
Booleancompare(Date start, String end)
compare
SimpleDateFormat format = new SimpleDateFormat(dtLong);
String s1 = format.format(start);
String s2 = end;
java.util.Calendar c1 = java.util.Calendar.getInstance();
java.util.Calendar c2 = java.util.Calendar.getInstance();
try {
    c1.setTime(format.parse(s1));
    c2.setTime(format.parse(s2));
...
intcompare(Date startDate, Date endDate, Date targetDate)
This method compares a target date with a date range.
return (compare(startDate, endDate, targetDate.getTime()));