Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between java.util.Date and Joda Time APIs [duplicate]

Possible Duplicate:
Should I use Java date and time classes or go with a 3rd party library like Joda Time?

I could use some guidance about when it's worth using Joda Time rather than the basic java.util.Date class. What are the benefits? Does Joda Time let you do anything you can't do with the Date class, or is it just easier to work with or what?

Also, I've seen conflicting info about whether Joda Time is part of the standard API. Is it standard or not?

like image 389
Sai Ye Yan Naing Aye Avatar asked Aug 20 '12 03:08

Sai Ye Yan Naing Aye


People also ask

What can I use instead of Java Util date?

The standard alternate is using the Calendar Object. Calendar has one dangerous point (for the unwary) and that is the after / before methods. They take an Object but will only handle Calendar Objects correctly. Be sure to read the Javadoc for these methods closely before using them.

Is Java Util calendar deprecated?

util. Date are deprecated since Java 1.1, the class itself (and java. util. Calendar , too) are not officially deprecated, just declared as de facto legacy.

Is Java Util date thread safe?

Not thread safe − java. util. Date is not thread safe, thus developers have to deal with concurrency issue while using date. The new date-time API is immutable and does not have setter methods.

What is Joda-time used for?

Joda-Time is the most widely used date and time processing library, before the release of Java 8. Its purpose was to offer an intuitive API for processing date and time and also address the design issues that existed in the Java Date/Time API.


1 Answers

Java's standard date and time classes (mainly java.util.Date and java.util.Calendar) have limited functionality and have a number of design problems. The fact that many of the constructors and methods of java.util.Date are deprecated is one indication of this.

Joda Time has a much better thought out API than what's available in Java's standard library. There are classes for timestamps with or without a timezone, classes for holding only a date (year, month, day) or only a time of day, classes for periods, durations and intervals, it supports the ISO 8601 format (which is the standard format in XML documents) and much more.

I'd use Joda Time as my standard date and time library in any project - there really is no reason to use java.util.Date and java.util.Calendar.

Note that in a future release of Java, a new date and time API will most likely be included that's going to look a lot like what's currently available in Joda Time; see Project ThreeTen.

Also see Why Joda Time? on the home page of the Joda Time project.

Also, I've seen conflicting info about whether Joda Time is part of the standard API. Is it standard or not?

No, it isn't part of the standard API.

Update (copied from the comments):

Java 8 is out now, and it has a new date and time API which is similar to Joda Time (see the package java.time). If you're using Java 8, then please use the new date & time API.

like image 157
Jesper Avatar answered Oct 23 '22 03:10

Jesper