Using Joda Time to ease testing
I would consider it a rarity for a business system not to make use of dates for some reason or another. Using the standard date libraries with the JDK is just a pain, one that I would prefer not to deal with. Fortunately, my current project team selected the Joda Time library as a replacement for java.util.Calendar, java.util.Date and theassociated parsers, formatters, etc available in the JDK. Beyond the ease of use of this library has compared to the standard API, our choice was further justified recently when we were asked to introduce a function to set the system time for testing purposes. Simply put, Joda Time allowed us to add this functionality without having to change any of our existing code.
To illustrate the ease of adding this functionality to our system, the following code is what is normally required to get the current system date:
\tYearMonthDay today = new YearMonthDay();
Adding the ability to change the date of our application (without actually changing the system clock) does not require a change to the above code. Instead a simple utility class has been provided in the Joda Time library, DateTimeUtils. By calling either setCurrentMillisFixed or setCurrentMillisOffset it is possible to change the date/time that the default YearMonthDay constructor uses for its internal state. DateTimeUtils.setCurrentMillisSystem can be used to restore the default bahaviour of using system clock to determine the value for the YearMonthDay default constructor.