Java Date API’s vs. Joda Time – What month is it?

As a java developer, I have a special place in my heart for the Java Date object and it’s related utility classes. In a series of posts I am going to exhort the benefits of using the Joda Time library and abandoning the Java date and time objects. From their site, the Joda Project is “…a Java open-source project that seeks to improve core Java functionality.” While they have a couple of other projects, the most active appears to be the Joda-Time project where the current version 1.6.1 was released on 2010-08-08.

We will start with a VERY basic example by comparing the simple task of pulling the current month full name and partial name. In Java there are several ways to do this, but I will choose to show a couple of the more common ways.

 

Java Calendar Object: Get the name of the current month:

 

As you can see, using the Java Calendar Object takes a bit of work to get the desired result. Oh, and if you also want short month names, you have to create that too.

Java SimpleDateFormat Object: Get the name of the current month:

 

Wow, so we have now effectively made the job a lot easier with the SimpleDateFormatter object. Or have we? You still end up having to create and work with objects which aren’t actual date objects. Klunky to me.

Joda-Time DateTime Object: Get the name of the current month:

 

Ahhhh, this is better. With the Joda-Time library it is as simple as calling a couple of methods to get the current month full name or short name. It really is intuitive and easy.

The Result:
Ok, so not a lot of complex code here, but this is just a sample of more to come. As someone who has struggled to find a happy life with the Java Date implementation I have found Joda-Time to be a breath of fresh air. Check out the Joda site and take the Joda-Time library out for a spin. I think you will like the results.

~Demian

Links:
Joda Time: joda-time-x.x.x.jar
Joda-Time overview: Joda-Time Main Site
Joda-Time Guide: Joda-Time User Guide
Joda-Time API: Joda-Time API

 

Crafted with love using:Crafted with love, using Desk!

Leave a Reply