Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are java.util.Date and java.util.Calendar deprecated?

It seems that the new java.time API offers everything from java.util.Date and much more.
Is there any reason to use java.util.Date when the newer java.time API is there since Java 8? Should java.util.Date and java.util.Calendar be avoided completely?

like image 595
Ondrej Bozek Avatar asked Feb 08 '18 15:02

Ondrej Bozek


People also ask

Is Java Util Calendar immutable?

util. Calendar APIs are not thread safe and lead to potential concurrency issues, adding additional headaches to handle thread safety. Date and Time APIs in Java 8 are immutable and therefore thread safe.

What is the difference between date and Calendar in Java?

What is difference between Java Date and Calendar classes? The difference between Date and Calendar is that Date class operates with specific instant in time and Calendar operates with difference between two dates.

Is Java Util Calendar thread-safe?

In short, the Calendar class is not thread-safe, and GregorianCalendar isn't either because it inherits the non-thread-safe fields and methods.

What is Java Util Calendar?

The java.util.calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.Following are the important ...


1 Answers

Short answer: The new API java.time is way better than the old world with java.util.Date and java.util.Calendar. So yes, the new API should be preferred in new code.

For a quick overview: Once I had written a comparison of features in table form for various date-time-libraries. There is almost no feature which java.time is missing but exists in the old world:

  • configurable gregorian/julian cutover
  • printing based on the class FieldPosition (used in Swing-component FormattedTextField)

About deprecation: Although most parts of java.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. The support of the old classes is still important for the goal of backwards compatibility with legacy code. So Oracle will probably not stop the support at any time in the future. But maybe Oracle will apply more sophisticated deprecation strategies.

Future development: It is interesting that the release of Java-8 has not only incorporated a completely new date/time-API (java.time) but also seen some few enhancements to java.util.Calendar, for example Calendar.Builder or SHORT_STANDALONE etc. Well, I can only speculate but this also seems to indicate that Oracle is not willing to stop the support of the old API in the near future.

like image 137
Meno Hochschild Avatar answered Sep 28 '22 09:09

Meno Hochschild