When writing an enterprise application, I often need to process the date. And in my latest project-Insurance industry-correcting date calculations is particularly important. Using Java.util.Calendar makes me a little uneasy. If you've ever used this class to deal with date/time values, then you know how troublesome it is to use it. So when I come in contact with an alternative to the joda-time--oriented date/Time library for Java applications-I decided to look into it. The result: I'm glad I did.
Joda-time makes time and date values easier to manage, manipulate, and understand. In fact, easy to use is the main design goal of Joda. Other goals include scalability, a complete set of features, and support for multiple calendar systems. And Joda is completely interoperable with JDK, so you don't have to replace all the Java code, just the part of the code that performs date/time calculations.
Joda Large Projects
Joda is actually a large project that covers numerous alternative APIs for the Java language, so technically, using Joda and joda-time names to denote the same meaning is a mistake. But at the time of writing this article, the Joda-time API appears to be the only Joda API in active development state. Given the current state of Joda large projects, I think it would be OK to joda-time for short Joda.
This article describes and shows you how to use it. I will introduce the following topics:
Introduction to date/time substitution library
Key concepts of Joda
Creating Joda-time Objects
Manipulating time style in a Joda manner
Format the time in a Joda manner
You can download the source code for the sample application that demonstrates these concepts.
Joda Introduction
Why do you use Joda? Consider creating a casual moment in time--for example, January 1, 2000 0:0. How do I create a JDK object that represents this moment in time? Using Java.util.Date? In fact, this is not feasible, since every Java version of Javadoc since JDK 1.1 declares that Java.util.Calendar should be used. The number of constructors that are not favoured using in Date severely limits the way you create such objects.
However, Date does have a constructor that you can use to create an object that represents an instant in time (except "Now"). The method corrects the time zone using the number of milliseconds since the Greenwich Mean Time (also known as epoch) from January 1, 1970 to be a parameter. Given the importance of Y2K to the software development business, you might think I've memorized this value-but I didn't. So is Date.
So what about the Calendar? I will create the required instances in the following ways:
Calendar calendar = Calendar.getInstance();
calendar.set(2000, Calendar.JANUARY, 1, 0, 0, 0);
With Joda, the code should resemble the following:
datetime datetime = new DateTime (2000, 1, 1, 0, 0, 0, 0);
This simple line of code is not much different. But now I'm going to complicate the problem a little bit. Suppose I want to add 90 days to this date and output the results. With JDK, I need to use the code in Listing 1:
Listing 1. Add 90 days to a certain moment as JDK and output the results
Calendar calendar = Calendar.getInstance();
calendar.set(2000, Calendar.JANUARY, 1, 0, 0, 0);
SimpleDateFormat sdf =
new SimpleDateFormat("E MM/dd/yyyy HH:mm:ss.SSS");
calendar.add(Calendar.DAY_OF_MONTH, 90);
System.out.println(sdf.format(calendar.getTime()));