views:

860

answers:

6

Is it a valid way of comparing dates:

Calendar someCalendar1 = Calendar.getInstance(); // current date/time
someCalendar1.add(Calendar.DATE, -14);

Calendar someCalendar2 = Calendar.getInstance();
someCalendar2.setTime(someDate); // someDate is in the format of MM/dd/yyyy

if(someCalendar2.compareTo(someCalendar1) < 0){
   ...Code...      
}

...or is there a better approach?

+1  A: 

Yes it is.

Zed
A: 

checkout Apache DateUtils

Nico
+2  A: 

Date implements comparable itself so there's no reason to wrap it into calendar:

Calendar someCalendar1 = Calendar.getInstance(); // current date/time
someCalendar1.add(Calendar.DATE, -14);

if (someDate.compareTo(someCalendar1.getTime()) < 0) {
   ...Code...                           
}

Date also has convenient after() and before() methods that make the above comparison easier to read:

if (someDate.before(someCalendar1.getTime())) {
   ...Code...                           
}

Finally, if you're dealing with date / time a lot, do consider using Joda Time instead of built-in java classes. It's MUCH more convenient and functional:

DateTime dt = new DateTime().minusWeeks(2);
if (new DateTime(someDate).isBefore(dt)) {
   ...Code...                           
}
ChssPly76
Thanks a bunch.. :)
+2  A: 

It's valid, but you're slightly confused about someDate - Calendar.setTime takes a java.util.Date, which is just a wrapper around a long indicating the number of milliseconds since midnight Jan 1st 1970, UTC. It's not "in the format MM/dd/yyy" - that's a string representation, not a java.util.Date. If it happens to print something out in the format MM/dd/yyyy, that's just what Date.toString is doing for you - it's not inherently part of the format.

As an aside, I would personally recommend that you avoid java.util.Date and java.util.Calendar completely and use Joda Time instead. It's a much better API.

Jon Skeet
+1 for difference between value/formatting
prateek urmaliya
Cool.. Thanks.. :)
A: 

It's OK. Also you can use before() and after():

package demo.so;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Tester {

    public static void main(String[] args) throws Exception {

    Calendar someCalendar1 = Calendar.getInstance(); // current date/time
    someCalendar1.add(Calendar.DATE, -11);

    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
    Date someDate =   df.parse("10/08/2009");

    Calendar someCalendar2 = Calendar.getInstance();
    someCalendar2.setTime(someDate);

    String cal1 = df.format(someCalendar1.getTime());
    String cal2 = df.format(someCalendar2.getTime());

    if (someCalendar1.equals(someCalendar2))
        System.out.println( cal1 + " is the same as " + cal2);
    if (someCalendar1.after(someCalendar2))
        System.out.println(cal1 + " is after " + cal2);
    if (someCalendar1.before(someCalendar2))
        System.out.println(cal1 + " is before " + cal2);

    }

}

But you shouldn't use Date, is deprecated and a source of troubles with dates handling. Build your own wrapper for GregorianCalendar or use some good library, like Joda.

JuanZe
+1  A: 

As one example of why Joda is better, take Daylight Savings Time.

If you're measuring "one day" as 1000 * 60 * 60 * 24 milliseconds, the Date library - and the Calendar library - both forget that there's one day in the year with 25 hours, and another with 23. You will occasionally screw up date calculations if you rely solely on the Java classes built into the J2SE API.

Joda is half likely to be the drop-in replacement for GregorianCalendar, Calendar, and Date in a future version of Java.

Dean J
JSR-310 isn't exactly the same as Joda, although it's very similar due to being spear-headed by the same person. It can't arrive fast enough IMO.
Jon Skeet