为什么大多数 java.util.Date 方法都被弃用了?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2901262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:20:30  来源:igfitidea点击:

Why were most java.util.Date methods deprecated?

javadatetimedatedeprecated

提问by Daniel Kaplan

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

当您查看 java.util.Date 类的 javadoc 时,大多数方法都已弃用。为什么这样做?

采纳答案by Yishai

Well, for two related reasons. It was a very poor implementation of the concept of Dates and Times and it was replaced by the Calendarclass.

嗯,有两个相关的原因。这是日期和时间概念的一个非常糟糕的实现,它被Calendar类所取代。

The Calendarclass, although an improvement, leaves a lot to be desired as well, so for serious Date/Time work, everyone recommends Joda-Time. Java 8brings the new java.time.* package, inspired by Joda-Time, defined by JSR-310, and intended to supplant the old Date/Calendar classes.

Calendar课程虽然有所改进,但仍有很多不足之处,因此对于认真的 Date/Time 工作,每个人都推荐Joda-TimeJava 8带来了新的java.time.* 包,受 Joda-Time 的启发,由JSR-310定义,旨在取代旧的 Date/Calendar 类。

Edit: In response to the specific question of why the implementation is poor, there are many reasons. The JavaDoc sums it up as follows:

编辑:针对为什么执行不佳的具体问题,有很多原因。JavaDoc 总结如下:

Unfortunately, the API for these functions was not amenable to internationalization.

不幸的是,这些函数的 API 不适合国际化。

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 DateFormatand the inability to have a non-Gregorian calendar representation), there are specific issues which really hurt the Dateclass, including the fact that year is presented in an offset of 1900 from Common Era year.

除了这个普遍的缺陷(包括缺少时区组件以及更好处理的日期格式DateFormat以及无法使用非公历表示等问题)之外,还有一些特定的问题确实会伤害到Date类,包括该年份以与 Common Era 年份 1900 的偏移量呈现的事实。

Calendarhas its own problems, but even as early as JDK 1.1 it was obvious that java.util.Datewas not going to cut it. Even though Calendaris arguable the worst JDK API, it has taken until version 7 to attempt to address it.

Calendar有其自身的问题,但即使早在 JDK 1.1 中,它也显然java.util.Date不会削减它。尽管Calendar可以说是最糟糕的 JDK API,但直到版本 7 才试图解决它。

回答by Bozho

  • Dateis mutable
  • Datedoesn't have support for time zones
  • Date是可变的
  • Date不支持时区

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)

后者导致它被Calendar. 而前者,结合易用性,导致两者都被Joda-Time/ JSR-310( java.time.* package)取代

回答by Will Hartung

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.

它们已被弃用,因为 Date 是在他们想要将 JDK 推出门外的那一天尽可能快地编写的。

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.

事实证明日期和日历很难。因此,他们创建了 Calendar 类,这是为了处理使用日历的困难部分。

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.

他们不赞成使用 Date 方法并将其委托给 Calendar,因为他们不想更改现有 Date 方法的行为,并且可能破坏现有应用程序。

回答by Daniel Kaplan

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

这是直接来自 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.Dateand 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.Datestart at 1900, months start at 1, and days start at 0—not very intuitive.

... java.util.Daterepresents 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.

Java 开发人员的一个长期问题是对普通开发人员的日期和时间用例的支持不足。

例如,现有的类(如java.util.DateSimpleDateFormatter)不是线程安全的,这会给用户带来潜在的并发问题——一般开发人员在编写日期处理代码时不会期望处理这些问题。

一些日期和时间类也表现出相当糟糕的 API 设计。例如,年份java.util.Date从 1900 开始,月份从 1 开始,天从 0 开始——不是很直观。

...java.util.Date代表时间线上的一个瞬间——一个包装自 UNIX 时代以来的毫秒数——但如果你调用 toString(),结果表明它有一个时区,导致开发人员混淆。

回答by Raibaz

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

我不知道它被弃用的官方原因,但据我所知GregorianCalendarJoda-Time支持对日期的操作,这意味着您可以添加,例如,日期的一天,并有它的月份和年份相应更新。

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.

例如,假设您要计算当前日期之后的第二天,而今天是 5 月 31 日;with java.util.Date, you just have getDays() +1, 返回32,你必须自己处理当月没有32天的知识;使用GregorianCalendar或 Joda.time,在 5 月 31 日添加一天会产生代表 6 月 1 日的对象,从而隐藏了您视线中的复杂性。