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?
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 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.
In short, the Calendar class is not thread-safe, and GregorianCalendar isn't either because it inherits the non-thread-safe fields and methods.
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 ...
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With