I know that making fun of the java.util.Calendar class is as cheap as
laughing at the Darwin award
winners
, but I spent a half hour this morning
debugging the following:

Calendar cal = Calendar.getInstance();
cal.set(2006, 12, 30, 8, 42);
Assert.assertEquals(2006, cal.get(Calendar.YEAR)); // FAIL

Here is what’s wrong with Calendar in this example, in order of increasing moronityTM:

  • Indexing months starting at 0.
  • Not throwing an exception when any of the passed values is invalid.
  • When a passed value is invalid, returning a valid date that has nothing to do
    with the values passed in set().

And these lessons come from a three-line code sample using Calendar,
which should give you a good idea why this class contains some of the worst code ever
included in the JDK (only second to Vector extending Stack).

What’s your vote for the worst JDK class?