I'm creating a web based system which will be used in countries from all over the world. One type of data which must be stored is dates and times.
What are the pros and cons of using the Java date and time classes compared to 3rd party libraries such as Joda time? I guess these third party libraries exist for a good reason, but I've never really compared them myself.
EDIT: Now that Java 8 has been released, if you can use that, do so!
java.time
is even cleaner than Joda Time, in my view. However, if you're stuck pre-Java-8, read on...Max asked for the pros and cons of using Joda...
Pros:
java.time
in Java 8, as they're at least somewhat similarCons:
DateTimeZoneBuilder
effectively in the past. This is a very rare use case though.To respond to the oxbow_lakes' idea of effectively building your own small API, here are my views of why this is a bad idea:
The answer is: it depends
JODA (and JSR-310) is a fully-functional date/time library, including support for use with multiple calendar systems.
Personally I found JODA to be a step too far in terms of complexity for what I need. The 2 principal (IMHO) mistakes in the standard java
Date
andCalendar
classes are:Although these are addressed by JODA, you'll find it quite easy to roll your own classes for
YearMonthDay
andInstant
, which both use the java classes under the hood for actual "calendrical" calculations. Then you don't have to familiarize yourself with an API of >100 classes, a different formatting/parsing mechanism etc.Of course, if you do need complete representation of different chronologies (e.g. Hebrew) or wish to be able to define your own imaginary Calendar system (e.g. for a game you are writing) then perhaps JODA or JRS-310 is for you. If not, then I would suggest that rolling your own is possibly the way to go.
The JSR-310 spec lead is Stephen Colebourne who wrote JODA in the 1st place, so will logically replace JODA.
You should use a Joda-Time library, because:
date representation.
Exception in thread "main" org.joda.time.IllegalFieldValueException: Cannot parse "2014-02-31": Value 31 for dayOfMonth must be in the range [1,28].
You may like this page for more details: http://swcodes.blogspot.com/
It all depends on what you are doing with the dates. If you are simply persisting them, them Java's built in Dates will probably do all you want them to. However if you are doing extensive time date manipulation, you're probably better off with Joda.
Well, unless you intend to wait for Java 8, hoping that they will implement a better API for manipulating date and time, yes, please, use Joda-Time. It's time saving and avoid many headaches.