Français - English
Source (plain): svn://svn.saintamh.org/code/lib-java/trunk/src/org/saintamh/date/
Source (highlighted): http://svn.saintamh.org/code/lib-java/trunk/src/org/saintamh/date/
2015 update: I wrote this years ago, and Oracle has since released Java 8, including the java.time package, which removes the need for this code. I'm leaving it here just to show how smart I am and how I fixed it before them, ha!

This package is meant to provide an alternative to the standard java.util.Date and java.util.Calendar classes. I wanted something like Python's datetime module, or Perl's DateTime. In particular I wanted:

 

Example usage:

    Date d1 = new Date (1979, 5, 27);
    Date d2 = new Date (1986, 8, 16);

    // The objects implement Comparable, so can be sorted using the usual methods:
    assert d1.compareTo(d2) < 0;

    // strftime and strptime allow conversion to/from strings
    assert d1.strftime("%Y-%m-%d").equals ("1979-05-27");
    assert Date.strptime("1979-05-27", "%Y-%m-%d").equals(d1);

    // The objects are immutable and have proper definitions of hashCode and
    // equals, so they're safe to use as HashMap keys:
    HashMap<Date,String> dateMap = new HashMap<Date,String>();
    dateMap.put (d1, "d1");
    dateMap.put (d2, "d2");
    assert dateMap.get(d1) == "d1";
    assert dateMap.get(d1) != "d2";

    // "delta" objects simplify date arithmetic
    DateDelta delta = d2.difference(d1);
    asset d1.add(delta).equals (d2);

 

This example only uses the Date class, but Time and DateTime behave similarly, as you'd expect. See somewhat more complete tests in DateTests.java.

See it in action e.g. in the FIP source code.