Why were most java.util.Date methods deprecated?

后端 未结 5 958
野趣味
野趣味 2020-11-27 18:36

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

相关标签:
5条回答
  • 2020-11-27 19:17
    • Date is mutable
    • Date doesn't have support for time zones

    The latter led to it being replaced by Calendar. And the former, combined with the ease-of-use, lead to both being replaced by Joda-Time / JSR-310 (java.time.* package)

    0 讨论(0)
  • 2020-11-27 19:18

    Here's a good answer straight from Oracle: http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html

    A long-standing bugbear of Java developers has been the inadequate support for the date and time use cases of ordinary developers.

    For example, the existing classes (such as java.util.Date and SimpleDateFormatter) aren’t thread-safe, leading to potential concurrency issues for users—not something the average developer would expect to deal with when writing date-handling code.

    Some of the date and time classes also exhibit quite poor API design. For example, years in java.util.Date start at 1900, months start at 1, and days start at 0—not very intuitive.

    ... java.util.Date represents an instant on the timeline—a wrapper around the number of milli-seconds since the UNIX epoch—but if you call toString(), the result suggests that it has a time zone, causing confusion among developers.

    0 讨论(0)
  • 2020-11-27 19:19

    I don't know the official reason why it has been deprecated, but as far as I can tell GregorianCalendarand Joda-Time support operations on dates, meaning that you can add, for instance, a day to a date and have its month and year updated accordingly.

    For instance, say you want to compute the day after the current date and today is May 31st; with java.util.Date, you just have getDays() +1, which returns 32, and you have to handle the knowledge that the current month doesn't have 32 days by yourself; with GregorianCalendaror Joda.time, adding a day to May 31st results in an object representing June 1st, hiding the complexity from your sight.

    0 讨论(0)
  • 2020-11-27 19:24

    They're deprecated because Date was written as fast as possible back in the day when they wanted to rush the JDK out the door.

    It turns out the Dates and Calendars are Hard. So, they created the Calendar class, which much more thought, in order to handle the Hard Parts of working with calendars.

    They deprecated the Date methods and delegated to Calendar because they didn't want to change the behavior of the existing Date methods, and possibly break existing applications.

    0 讨论(0)
  • 2020-11-27 19:29

    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.

    0 讨论(0)
提交回复
热议问题