When you look at the javadoc of the java.util.Date class, most of the methods are deprecated. Why was this done?
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.
util. Date (just Date from now on) is a terrible type, which explains why so much of it was deprecated in Java 1.1 (but is still being used, unfortunately). Design flaws include: Its name is misleading: it doesn't represent a Date , it represents an instant in time.
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.
Deprecated - Returns the year in the specified date according to local time. Use getFullYear instead. Sets the day of the month for a specified date according to local time. Sets the full year for a specified date according to local time.
Well, for two related reasons. It was a very poor implementation of the concept of Dates and Times and it was replaced by the Calendar
class.
The Calendar
class, although an improvement, leaves a lot to be desired as well, so for serious Date/Time work, everyone recommends Joda-Time. Java 8 brings the new java.time.* package, inspired by Joda-Time, defined by JSR-310, and intended to supplant the old Date/Calendar classes.
Edit: In response to the specific question of why the implementation is poor, there are many reasons. The JavaDoc sums it up as follows:
Unfortunately, the API for these functions was not amenable to internationalization.
In addition to this general deficiency (which covers issues like the lack of a Time Zone component as well as the date formatting which is better handled in DateFormat
and the inability to have a non-Gregorian calendar representation), there are specific issues which really hurt the Date
class, including the fact that year is presented in an offset of 1900 from Common Era year.
Calendar
has its own problems, but even as early as JDK 1.1 it was obvious that java.util.Date
was not going to cut it. Even though Calendar
is arguable the worst JDK API, it has taken until version 7 to attempt to address it.
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