为什么 java.util.Date 的 setTime() 方法没有被弃用?

发布于 2024-08-07 18:01:31 字数 196 浏览 2 评论 0 原文

所有其他修改器在 JDK 1.1 中都已被弃用,那么为什么 setTime() 保持原样呢?当然,java.util.Calendar - 操作日期的正确方法 - 可以根据需要使用 java.util.Date 创建 java.util.Date 的新实例(长) 构造函数?

All the other mutators got deprecated back in JDK 1.1, so why was setTime() left as is? Surely java.util.Calendar - the correct way to manipulate dates - can create new instances of java.util.Date as needed using the java.util.Date(long) constructor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

被翻牌 2024-08-14 18:01:31

已弃用的 Date 位是与日历相关的位(即日、月、年等)。您会注意到,日历字段的访问器方法也被弃用,而不仅仅是修改器。

然而,毫秒值的表示形式仍然是 Date 的工作方式,并且与日历表示形式无关,因此它仍然未弃用。

The bits of Date that were deprecated were those bits that related to calendars (i.e. day, month, year, etc). You'll note that the accessor methods for calendar fields are also deprecated, not just the mutators.

The representation as a milliseconds value, however, is still the way Date works, and is unrelated to the calendar representation, so it stayed as undeprecated.

老旧海报 2024-08-14 18:01:31

其他变异器尝试使用 java.util.Date 作为日历,而不是作为时间的瞬间,自 1970 年 1 月 1 日凌晨 12 点(UTC)以来的毫秒数。因此,不弃用该变异器是有道理的。

当然,Date/Calendar API 仍然很糟糕,您仍然应该使用 Joda Time 尽可能 - 但我明白为什么这个调用没有被弃用。你不能在事后使类型不可变,这不是弃用的重点 - 重点是试图阻止人们使用它作为“1976 年 6 月 19 日”等的存储。

The other mutators were trying to use java.util.Date as a calendar, rather than as an instant in time, wrapping a number of milliseconds since Jan 1st 1970 12am UTC. As such, it makes sense for that one mutator not to be deprecated.

The Date/Calendar APIs are still terrible of course, and you should still use Joda Time wherever possible - but I can see why that call wasn't deprecated. You can't make a type immutable after the fact, and that wasn't the point of the deprecation - the point was to try to stop people from using it as storage for "June 19th 1976" etc.

深海不蓝 2024-08-14 18:01:31

因为 setTime 方法至少在逻辑上是正确的:需要 daymonth 等的 setX 方法没有任何意义:java Date 是一个时间,因此是小时 > 等仅与特定TimeZone 中该Date 的视图相关。

Because the setTime method is at least logically correct: the setX methods which take day, month etc make no sense: a java Date is an instant in time and hence day, hour, month etc relate only to the view of that Date in a particular TimeZone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文