Comparing dates ignoring the seconds and the milliseconds instant of DateTime in Joda

Tiny picture Tiny · Feb 19, 2013 · Viewed 14k times · Source

Let's assume that I have two dates like the following.

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm:ss").withZone(DateTimeZone.forID("Asia/Kolkata"));
DateTime firstDate = formatter.parseDateTime("16-Feb-2012 12:03:45");
DateTime secondDate = formatter.parseDateTime("17-Feb-2013 12:03:45");

I want to compare these two dates to see whether firstDate is sooner, later or equal to secondDate.

I could try the following.

System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.isBefore(secondDate));
System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.isAfter(secondDate));
System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.equals(secondDate));

What is produced by this code is exactly what I want.

It produces the following output.

firstDate = 2012-02-16T12:03:45.000+05:30
secondDate = 2013-02-17T12:03:45.000+05:30
comparison = true

firstDate = 2012-02-16T12:03:45.000+05:30
secondDate = 2013-02-17T12:03:45.000+05:30
comparison = false

firstDate = 2012-02-16T12:03:45.000+05:30
secondDate = 2013-02-17T12:03:45.000+05:30
comparison = false

I need to ignore the seconds and the milliseconds portion of these dates. I have tried to use the withSecondOfMinute(0) and withMillis(0) methods like the following.

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm:ss").withZone(DateTimeZone.forID("Asia/Kolkata"));
DateTime firstDate = formatter.parseDateTime("16-Feb-2012 12:03:45").withSecondOfMinute(0).withMillis(0);
DateTime secondDate = formatter.parseDateTime("17-Feb-2013 12:03:45").withSecondOfMinute(0).withMillis(0);        

But it yielded the following output.

firstDate = 1970-01-01T05:30:00.000+05:30
secondDate = 1970-01-01T05:30:00.000+05:30
comparison = false

firstDate = 1970-01-01T05:30:00.000+05:30
secondDate = 1970-01-01T05:30:00.000+05:30
comparison = false

firstDate = 1970-01-01T05:30:00.000+05:30
secondDate = 1970-01-01T05:30:00.000+05:30
comparison = true

The docs of the withSecondOfMinute() method describes.

Returns a copy of this datetime with the second of minute field updated. DateTime is immutable, so there are no set methods. Instead, this method returns a new instance with the value of second of minute changed.

And the docs of the method withMillis() says.

Returns a copy of this datetime with different millis. The returned object will be either be a new instance or this. Only the millis will change, the chronology and time zone are kept.

Comparing dates by ignoring the time portion completely can easily be done using DateTimeComparator.getDateOnlyInstance() roughly like the following.

if(DateTimeComparator.getDateOnlyInstance().compare(firstDate, secondDate)==0){}
if(DateTimeComparator.getDateOnlyInstance().compare(firstDate, secondDate)<0){}
if(DateTimeComparator.getDateOnlyInstance().compare(firstDate, secondDate)>0){}

How to compare two dates ignoring the specific instant in DateTime (seconds and milliseconds in this case)?

Answer

Steinar picture Steinar · Feb 19, 2013

Another approach is to create a DateTimeComparator with minute as lower limit:

DateTimeComparator comparator = DateTimeComparator.getInstance(
         DateTimeFieldType.minuteOfHour());

which would ignore the second and millisecond parts of the objects being compared.