Indrek Ots
by Indrek Ots
~1 min read

Categories

  • articles

Tags

  • java
  • joda-time
  • jdk
  • testing
  • unittesting

Prior to Java 8, the date and time classes included in the standard library are considered by many flawed. They’re difficult for developers to use. For example, java.util.Date and java.util.Calendar are mutable, which is inconvenient in multi-threaded environments. I think this StackOverflow question has some great answers explaining the situation.

Joda-Time is a Java library which provides a quality replacement for the Java date and time classes. From Java SE 8 onwards, users are asked to migrate to java.time (JSR-310).

Using Date and Calendar classes in unit tests has one drawback. It is not possible to set the system time. Luckily Joda-Time provides an API to do that.

DateTimeUtils.setCurrentMillisFixed()

If you decide to use that, be sure to reset the real system time in a tearDown() method. Otherwise there’s a chance you have some other tests which expect the system time be the actual current time and depending on the test execution order you might see unexpected results.