Java WTF: Calendar vs Date

I’ve been doing some date math at work and I came across something I thought was wack.

Check this out:

Calendar cal1 = Calendar.getInstance();
Thread.sleep(500);
Calendar cal2 = Calendar.getInstance();
Thread.sleep(500);
Date now = new Date();

System.out.println(cal1.before(cal2));
System.out.println(cal1.before(now));

What do you think this snippet prints (It compiles, I promise!)?

Looking at the code, it shows that we are getting three snapshots in time, waiting half a second between snapshots, and assigning those values to cal1,cal2, and now, respectively. I put in the Thread.sleep() to illustrate the point more clearly. The first print statement compares the first and second snapshots in time, returning true if the first is before the second. The second print statement returns true if the first is before the third snapshot.

Logically, we would expect both print statements to prints true. However, this outputs:

true
false

But, how could the first snapshot be before the second snapshot but not the third?

Putting up the javadocs, gives us our answer. The Calendar before and after methods always returns false if you don’t pass it a Calendar object. Why? I have no idea. Why not just have the method signature take a Calendar object? I mean, I guess this allows you to extend Calendar to be able to call after/before on whatever kind of classes you want to support…but does it really make sense to compare a Calendar to a Monkey class? This is what I call a Java WTF.

Java WTF: Calendar vs Date

9 thoughts on “Java WTF: Calendar vs Date

  1. Gareth says:

    Here is the code behind Calendar.before(Object when)

    public boolean before(Object when) {
    return when instanceof Calendar &&
    getTimeInMillis()

  2. I’m interested in hearing the Java API designers rationale on why they designed the Calendar API like this. I understand the implementation. Don’t you think it’s kind of strange the Date class before/after takes a Date object and performs as expected, while the Calendar class’s after/before takes any ol’ Object AND only operates on other Calendar objects?

  3. Joda-time is great, however for anything that requires a Date or Calendar object you’ll just wind up have to screw around with conversion back to Date and Calendar.

    Sun needs to have a flag day at some point and just throw out both Date and Calendar. It’s one of the single worst aspects of Java programming.

  4. whaley says:

    Joda-time is being used as the basis for the date/time/whatever api in jdk7. David HM Spector, you are getting your wish.

  5. JSR-310 is a clean up of Joda-Time that will hopefully be part of Java 7, so it would be a good idea to start using it.

    I didn’t know that Date/Calendar was an IBM contribution. Do you have a reference? It would explain many things (even though I’m not saying Sun does always perfect thing, of course).

Leave a Reply to bhaskar Cancel reply

Your email address will not be published. Required fields are marked *