问题描述
我经常遇到Java Date
和其他日期时间相关类的负面反馈。作为一个.NET开发人员,我完全不了解(没有使用它们)了解,他们实际上是错误的。 任何人都可以看出这一点吗?
Java 日期
类。也许是一个最好的例子之一,如何不用任何语言,任何地方做某事。我在哪里开始?
阅读JavaDoc可能会让人认为开发人员实际上有一些好的想法。关于和。 com / 2008/12 / 109876543211happy-new-year.htmlrel =noreferrer> pretty )。
然而,设计决策真的浪费了任何想法是一个精心设计的API。以下是一些最喜欢的错误:
- 尽管在千年的最后十年被设计,自1900年以来,它的年数是两位数。由于这个平淡无奇的决定,在Java世界中,1900年(或1900年)的解决方法已经成百上千万。
- 几个月零索引,以满足惊人的异常情况有一个数组的月份,并没有生活与十三个元素数组,其中第一个包含一个
null
。因此,我们有0..11(今天是109年的第11个月)。为了转换成一个字符串,有几个++类似的数字。 - 他们是。因此,任何时候你想要回溯(比如,作为一个实例结构),你需要返回该日期的克隆,而不是日期对象本身(否则人们可以改变你的结构)。
- 旨在修复这个的
日历
实际上造成同样的错误。他们仍然是可变的。 -
日期
代表一个DateTime
,但在还有另一个子类java.sql.Date
,代表一天(尽管没有与之相关的时区)。 - 没有与
Date
相关联的TimeZone
,因此范围(例如整天)通常表示为午夜 - 午夜(通常在某个任意时区)
最后,值得注意的是,秒钟通常会对一个在一小时内用ntp更新的良好的系统时钟进行修正(见下面的链接)。引入两个闰秒(每六个月最少几个月,几乎每几年),系统的机会是不太可能的,特别是考虑到您必须不时重新部署新版本的代码。即使使用重新生成类或类似WAR引擎的动态语言,也会最终污染类空间并耗尽其他类。
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.
Can anybody shed some light on this?
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?
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).
However, the design decisions really lay to waste any thought of being a well designed API. Here are some of the favourite mistakes:
- 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 aDateTime
, but in order to defer to those in SQL land, there's another subclassjava.sql.Date
, which represents a single day (though without a timezone associated with it).- There are no
TimeZone
s associated with aDate
, and so ranges (such as a 'whole day') are often represented as a midnight-midnight (often in some arbitrary timezone)
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 below). 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& amp;时间API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!