How to use Joda-Time DateTimeComparator

2.3k Views Asked by At

I want to compare 2 dates without the time, some posts in SO suggest to use Joda-Time API, but I don't understand how to use Joda-Time. I can't find any post that include how to declare, what package to be imported. and what does method getDateOnlyInstance() return? An integer or what?

1

There are 1 best solutions below

2
On BEST ANSWER

As to how to use Joda API, google it, pretty basic stuff, you need to have the correct jar's on your classpath. Get the latest version of the jar here: http://sourceforge.net/projects/joda-time/files/joda-time/2.3/ (or use maven or some other build tool).

What the getDateOnlyInstance() returns is a comparator, which when passed two DateTime-objects will compare them, but ignore the time-portion of the them, and only compare the dates (for instance, if you pass 2014-01-24 04:20:00 and 2014-01-24 13:37:00 they will be regarded as equal.)

DateTime is one of the central classes in JodaTime, and it is a representation of a date and a time (duh), much like the java.util.Date, but with one big difference: DateTime is actually usable. You can do fun operations like adding and substracting days, hours and minutes, set the time-portion of it to midnight, and construct representations of dates without getting any grey hairs in the meantime.

So download the zip for JodaTime from the link above, and place the joda-time-2.3.jar on your classpath, then you can use both DateTime and its comparator.