Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why were most java.util.Date methods deprecated?

When you look at the javadoc of the java.util.Date class, most of the methods are deprecated. Why was this done?

like image 424
Daniel Kaplan Avatar asked May 25 '10 00:05

Daniel Kaplan


People also ask

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.

What is wrong with Java Util date?

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.

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.

Which is deprecated method in date object?

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.


1 Answers

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.

like image 90
Yishai Avatar answered Sep 22 '22 02:09

Yishai