tags:

views:

1721

answers:

9

Every time I need to work with date and/or timstamps in Java I always feel like I'm doing something wrong and spend endless hours trying to find a better way of working with the APIs without having to code my own Date and Time utility classes. Here's a couple of annoying things I just ran into:

  • 0-based months. I realize that best practice is to use Calendar.SEPTEMBER instead of 8, but it's annoying that 8 represents September and not August.

  • Getting a date without a timestamp. I always need the utility that Zeros out the timestamp portion of the date.

  • I know there's other issues I've had in the past, but can't recall. Feel free to add more in your responses.

So, my question is ... What third party APIs do you use to simplify Java's usage of Date and Time manipulation, if any? Any thoughts on using Joda? Anyone looked closer at JSR-310 Date and Time API?

A: 

Date APIs are very difficult to design, especially if they have to deal with localization. Try to roll your own and see, it's worth doing at least once. The fact that Joda was able to do such a good job is a real credit to its developers. To answer your question, I've heard nothing but good things about that library, though I have never played around with it myself.

Daniel Spiewak
+1  A: 

Im using GregorianCalendar - always and everywhere. Simple java.util.Date is too complex, yeah.

So, my advice is - use GC, its simple

Vugluskr
A: 

A lot of programmers begin by using Date, which has numerous deprecated overloaded constructors (making it difficult to use), but once you figure out GregorianCalendar it becomes a little bit easier to manage. The example here is pretty helpful:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/GregorianCalendar.html

hal10001
+8  A: 

This post has a good discussion on comparing the Java Date/Time API vs JODA.

I personally just use Gregorian Calendar and SimpleDateFormat any time I need to manipulate dates/times in Java. I've never really had any problems in using the Java API and find it quite easy to use, so have not really looked into any alternatives.

Rich Adams
Joda ref: http://joda-time.sourceforge.net/
Arthur Thomas
Parts of Joda may be integrated into Java 7: http://today.java.net/pub/a/today/2008/09/18/jsr-310-new-java-date-time-api.html
Michael Angstadt
A: 

It's the same in javascript. Someone must have been smoking something when they think it's a good idea to let 2008 mean the year 2008, 31 to mean the 31st day in the month, and - this is the best part - 11 to mean the 12th month.

On the other hand, they got it right on two out of three.

Thomas Eyde
+1  A: 

The thing that always gets me with Java is the date time library. I've never used Joda, just briefly look at it, looks like its a pretty good implementation, and if I understand JSR-130 correctly its taking knowledge from Joda and eventually having it included in JavaSE.

Quite often for past projects I've wrapped the Java date time objects, which in itself was quite a task. Then used the wrappers for date manipulation.

ShaneB
A: 

It's really simple to write your own date API which sits on top of the raw Java classes, Date and Calendar. Basically both date and Calendar suffer from the fact that they are trying to cram two concepts into one class:

  1. Date (i.e. Year-Month-Day)
  2. Instant (i.e. currentTimeMillis)

When you understand this, it will just revolutionize how you handle date-like concepts in your code. Things will be simpler, clearer, better. In every sense!

For me, Joda is over-complicated, at least for the overwhelming majority of purposes and I particularly don't like the fact that they have gone against standard Java forms, one example being how they parse and format dates. Stephen Colebourne, the guy behind JODA, is the spec lead of JSR-310 and this suffers from the same problems imho (I've followed and contributed to the discussions for the last few years).

Do it yourself; it's easy. Just fill in the following classes: MyDate (wrapping year-month-day), Month (an enum), TimeOfDay (hour-min-sec-millis), DayOfWeek (enum), Instant (wrapping a long). Always consider time-zones when converting between Instants and Dates.

If this seems daunting, you can use Calendar and SimpleDateFormat under the hood. You'll do this in a day and never regret it.

oxbow_lakes
+2  A: 

The Apache Commons Lang project has a DateUtils class that performs helpful Date operations.

I use DateUtils.truncate() a lot, which will "zero out" parts of the Date for you (helpful if you want your Date object to, say, represent a date and not include any time information). Each method works for both Date and Calendar objects too.

http://commons.apache.org/lang/

Michael Angstadt
+2  A: 

I've been using Joda exclusively for three years now and would definitely recommend it - it has the whole area covered with an interface that 'does what it says'.

Joda can look complex when you start, as eg it has concepts of periods, duration and intervals which look sort of similar, but you can start off simply by substituting org.joda.time.DateTime (or org.joda.time.DateMidnight) for java.util.Date in your code, and sticking with the many useful methods that those classes contain, before understanding the other areas.

Patrick Wilkes