Java Date & 有什么问题?时间API? [英] What's wrong with Java Date & Time API?

查看:20
本文介绍了Java Date & 有什么问题?时间API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常遇到关于 Java Date 和其他与日期时间相关的类的负面反馈.作为 .NET 开发人员,我无法完全(没有使用过它们)理解它们究竟有什么问题.

Very often I come across negative feedback on Java Date and other date-time-related classes. Being a .NET developer, I cannot fully (without having used them) understand, what's actually wrong with them.

有人能解释一下吗?

推荐答案

啊,Java Date 类.也许是最好的例子之一,说明如何不在任何地方用任何语言做某事.我从哪里开始?

Ah, the Java Date class. Perhaps one of the best examples of how not to do something in any language, anywhere. Where do I begin?

阅读 JavaDoc 可能会让人认为开发人员实际上已经有了一些好主意.它继续讨论 UTCGMT 的长度,尽管事实上两者之间的差异基本上是闰秒(发生 漂亮 很少).

Reading the JavaDoc might lead one to think that the developers have actually got some good ideas. It goes on about the difference between UTC and GMT at length, despite the fact that the difference between the two is basically leap seconds (which happen pretty rarely).

然而,设计决策确实浪费了任何设计良好的 API 的想法.以下是一些最容易犯的错误:

However, the design decisions really lay to waste any thought of being a well designed API. Here are some of the favourite mistakes:

  • 尽管是在千禧年的最后十年设计的,但自 1900 年以来的年数为两位数.由于这个平庸的决定,Java 世界中有数百万种解决方法在 1900+(或 1900-)上运行.
  • 月份是零索引的,以适应非常不寻常的情况,即拥有一个月份数组而不是包含 13 个元素的数组,其中第一个元素包含 null.结果,我们有 0..11(今天是 109 年的第 11 个月).有类似数量的 ++ 和 -- 用于将月份转换为字符串.
  • 它们可变.因此,任何时候您想要返回日期(例如,作为实例结构),您都需要返回该日期的副本,而不是日期对象本身(否则,人们可以改变您的结构).
  • 旨在修复"此问题的 Calendar 实际上犯了同样的错误.它们仍然是可变的.
  • Date 代表一个 DateTime,但为了顺应 SQL 领域,还有一个子类 java.sql.Date,它代表一天(尽管没有与之关联的时区).
  • 没有与 Date 相关联的 TimeZone,因此范围(例如一整天")通常表示为午夜(通常在一些任意的时区)
  • Despite being designed in the last decade of the millennium, it rates years as two digits since 1900. There are literally millions of workarounds doing 1900+ (or 1900-) in the Java world as a result of this banal decision.
  • Months are zero indexed, to cater for the spectacularly unusual case of having an array-of-months and not living with a thirteen element array, the first of which containing a null. As a result, we have 0..11 (and today being month 11 of the year 109). There are a similar number of ++ and -- on the months in order to convert to a string.
  • They're mutable. As a result, any time you want to give a date back (say, as an instance structure) you need to return a clone of that date instead of the date object itself (since otherwise, people can mutate your structure).
  • The Calendar, designed to 'fix' this, actually makes the same mistakes. They're still mutable.
  • Date represents a DateTime, but in order to defer to those in SQL land, there's another subclass java.sql.Date, which represents a single day (though without a timezone associated with it).
  • There are no TimeZones associated with a Date, and so ranges (such as a 'whole day') are often represented as a midnight-midnight (often in some arbitrary timezone)

最后,值得注意的是,闰秒通常会根据良好的系统时钟进行自我校正,该时钟会在一小时内使用 ntp 更新(请参阅上面的链接).在引入两个闰秒(最少每六个月,实际上每隔几年)时系统仍然启动并运行的可能性很小,特别是考虑到您必须不时重新部署新版本的代码的事实.即使使用重新生成类的动态语言或诸如 WAR 引擎之类的东西也会污染类空间并最终耗尽 permgen.

Finally, it's worth noting that leap seconds generally correct themselves against a good system clock which is updated with ntp within an hour (see links above). The chance of a system being still up and running in the introduction of two leap seconds (every six months minimum, every few years practically) is pretty unlikely, especially considering the fact that you have to redeploy new versions of your code from time to time. Even using a dynamic language which regenerates classes or something like a WAR engine will pollute the class space and run out of permgen eventually.

这篇关于Java Date & 有什么问题?时间API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆