Q:

Compare dates ignoring milliseconds?

Is there a way to compare two calendar objects, but ignore milliseconds?
I have written a test case that compared two calendar objects, but there is a problem. Although all of the day, month, minutes and hours match, the milliseconds doesn't matches. I get the expected date before getting the real date:
/**
 * @return
 */
private Calendar getExpectedOneMonthDateFromCurrentDate() {
    Calendar expectedOneMonth = Calendar.getInstance();
    expectedOneMonth.add(Calendar.MONTH, -1);
    return expectedOneMonth;
}

assertEquals(getExpectedOneMonthDateFromCurrentDate(),
             DateRange.LAST_ONE_MONTH.getToDate());

A:

Remove milliseconds from your calendar
cal.set(Calendar.MILLISECOND, 0);

A:

You need to use
cal.set(Calendar.MILLISECOND, 0);

and possibly as well
cal.set(Calendar.SECOND, 0);

if you just need the minutes to match.

A:

The solution of setting the milliseconds to 0 has an issue: if the dates are 12:14:29.999 and 12:14:30.003, you will set the dates to 12:14:29 and 12:14:30 respectively and will detect a difference where you don't want to.
I have thought about a Comparator:
private static class SecondsComparator implements Comparator<Calendar>
{
    public int compare(Calendar o1, Calendar o2)
    {
        final long difference = o1.getTimeInMillis() - o2.getTimeInMillis();
        if (difference > -1000 && difference < 1000)
            return 0;
        else
            return difference < 0 ? 1 : -1;
    }
}

public static void main(String args[])
{
    Calendar c1 = Calendar.getInstance();
    Utils.waitMilliseconds(100);
    Calendar c2 = Calendar.getInstance();
    // will return 0 
    System.out.println(new SecondsComparator().compare(c1,c2));
}

However, it no a good solution neither, as this Comparator breaks the following rule:
The implementer must ensure that x.compareTo(y)==0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.
What leads to (x=y and y=z) => x=z.
So I don't see any solution... But indeed, if you define some different dates, they are different, aren't they?

