Sunday, January 9, 2011

Joda Time == Good Times

I heard about Joda Time a couple months back, but finally got to use and implement it in a project.

We all know the whole Java date thing is ugly, and I sure most people have the own "implementations" and extensions like Period objects and utility Date Diff classes. I know I've written my fair share over the years. I won't be doing that again...

I am not going to go into too much detail as the Joda documentation is pretty good.
I just wanna highlight some of the very usual functionality that won me over:

Setting the "System" time, very useful. The financial systems I have worked on throughout my career always had some concept that required the "Current Date" mostly for data and rules with effective date periods.

Joda lets you statically set the "Current Date" making it a pleasure to test with, ensuring that any new DateTime objects used will be based off the set date:


The classic Date diff:

Working with "parts", just the date or the time:


  1. I see a lot of static method invocations there - how thread safe would Hours.hoursIn() be in, say a super fast paced transactional processing environment for say, a financial institution?

    Are you going to have to make complicated thread local factories to prevent race conditions (say in a web service class that called a moer of a lot)?

  2. :) This exactly is why you use open source and don't write your own "implementations" ...

    To quote the Joda Time Documents:
    "Hours is thread-safe and immutable."


    Every public class in Joda-Time is documented as being thread-safe or not. Joda-Time makes heavy use of the immutability design pattern, and all immutable classes in Joda-Time are thread-safe. Many mutable classes whose instances are not likely to be shared are not thread-safe, and are documented as such.

    The most common multi-threading mistake made by Java programmers is in the use of SimpleDateFormat. Calling its format method on a shared instance by concurrent threads can produce bizarre results. All of Joda-Time's formatting classes are thread-safe and immutable. "


Popular Posts