Java provides two different APIs to deal with dates and times, java.util.Date and java.util.Calendar.
Both of these APIs are missing necessary operations that should be easily doable with dates and times, such as addition and substraction, They also lack several important classes such as time intervals, durations and periods. They don’t allow for the creation of time zone agnostic dates, either.
They have huge implementation failures, too: they are mutable. And they are not thread safe.
All of these issues came to my attention after having to deal with the newly implemented DST that came into effect last December here in Argentina.
Until then, I’ve always found easy-to-implement workarounds for any date or time problem I came across, by using the milliseconds since Epoch. But this gets far more dirty (as if it wasn’t dirty enough) when you have to take into account that some days last 23 hours and some others, 25. As stated by Paul Hill, the time offset for each Time Zone has to be taken into account.
Further investigation took me to a third party API for date and time handling, Joda. Although I’ve not used it myself yet, it seems to have a solution for every drawback Java’s APIs have. It’s a strong candidate to replace my Helper classes.
I’ll have to come back later to comment on Joda, after having tried it.
Anyways, the final solution should arrive with Java 7.
Last year, the Java Community Process approved Java Specification Request 310, Date and Time API, in an effort to provide a neat API right into the JDK.
This JSR is very promising. One of its spec leads is Joda’s project leader, Stephen Colebourne. This is a good thing, as his experience should be invaluable for this task. Also, they seem to be seriously listening for suggestions and feedback from about everyone.
However, the task at hand is a difficult one. Whoever wants to do some work with dates, must consider java.util.Date compatibility a must have, since there’s a strong dependency between JDBC and java.util.Date, as well as between JDBC and almost every business running a Java application.
JSR 310 looks like an excellent addition to the JDK. So far, the only complaint I have in this regard is timing (ironic, huh?). This should’ve been addressed years ago. But, better late than never.
- Losing Time on the Garden Path: Calculating the Difference Between Two Datetime Stamps
- Joda Time
- JSR 310: Date and Time API
- The Server Side discussion on JSR-310
- JSR-310 project page
There’s one more page I consulted, that I reached through Paul Hill’s page. This has little to do with Java, but it’s really interesting material. It’s called “The Best of Dates, the Worst of Dates”, and it deals with about every aspect of Calendars.