Should I use Java date and time classes or go with a 3rd party library like Joda Time?
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 similar Cons:
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:
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.
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
and Calendar
classes are:
Although these are addressed by JODA, you'll find it quite easy to roll your own classes for YearMonthDay
and Instant
, 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 (eg Hebrew) or wish to be able to define your own imaginary Calendar system (eg 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.
链接地址: http://www.djcxy.com/p/18766.html