tags:

views:

271

answers:

4

What is the Best Practice for manipulating and storing Dates e.g. using GregorianCalendar in an enterprise java application?

Looking for feedback and I will consolidate any great answers into a Best Practice that others can use. Thanks!

A: 

To get the discussion started, here's been my experience:

When creating standards for a typical 3-tier Java Enterprise project, I would generally recommend that the project use GregorianCalendar for manipulating dates. Reason is GregorianCalendar is the de facto standard over any other Calendar instance e.g. Julian calendar etc. It's the recognized calendar in most countries and properly handles leap years, etc. On top of that, I would recommend that the application store its dates as UTC so that you can easily perform date calculations such as finding the difference between two dates (if it were stored as EST for example, you'd have to take day light savings time into account). The date can be then be localized to whatever timezone you need it to be displayed to the user as -- such as localizing it to EST if you are an east-coast US company and you want your time information shown in EST.

BestPractices
"e.g. Julian calendar etc" You can leave this out, since it doesn't make any sense. There are too many corner cases to start listing things here. For example, Astronomers use an Astronomical Julian day number. They also use Gregorian Dates. Better to say less on this point. Also, it helps to use bullets for each of your points.
S.Lott
+7  A: 

Joda time (100% interoperable with the JDK)

Joda-Time provides a quality replacement for the Java date and time classes. The design allows for multiple calendar systems, while still providing a simple API

Bozho
+3  A: 

Joda is the way to go. Why ?

  1. it has a much more powerful and intuitive interface than the standard Date/Time API
  2. there are no threading issues with date/time formatting. java.text.SimpleDateFormat is not thread-safe (not a lot of people know this!)

At some stage the Java Date/Time API is going to be superseded (by JSR-310). I believe this is going to be based upon the work done by those behind Joda, and as such you'll be learning an API that will influence a new standard Java API.

Brian Agnew
Now if you can't use Joda (some projects are limited in what external JARs they can introduce to the project), is using java.util.GregorianCalendar to hold and manipulate dates (making sure you account for DST by doing something like maintaining your dates in UTC timezone) the best practice?
BestPractices
+3  A: 

The best practice is usually precisely NOT to think in term of heavy date objects but to store a point in time. This is typically done by storing a value that doesn't suffer from corner cases nor from potential parsing problems. To do this, people usually store the number of milliseconds (or seconds) elapsed since a fixed point that we call the epoch (1970-01-01). This is very common and any Java API will always allow you to convert any kind of date to/from the time expressed in ms since the epoch.

That's for storage. You can also store, for example, the user's preferred timezone, if there's such a need.

Now such a date in milliseconds, like:

System.out.println( System.currentTimeMillis() );
1264875453

ain't very useful when it's displayed to the end user, that's for granted.

Which is why you use, for example, the example Joda time to convert it to some user-friendly format before displaying it to the end-user.

You asked for best practice, here's my take on it: storing "date" objects in a DB instead of the time in milliseconds is right there with using floating point numbers to represent monetary amounts.

It's usually a huge code smell.

So Joda time in Java is the way to manipulate date, yes. But is Joda the way to go to store dates? CERTAINLY NOT.

Webinator