为什么 java.util.Calendar 不使用枚举?
有时,我会因 juCalendar 缺乏类型安全性而感到沮丧,并想知道在日历 API 中不使用枚举是否有一个让我无法理解的充分理由,或者唯一的原因是否是它可能是在枚举成为语言的一部分之前编写的,不值得破坏旧代码。
第二个子问题是:是否值得我花时间围绕 Calendar 类创建一个类型更安全的包装器,或者以前是否已经这样做过?
总结: Calendar
自 Java 1.1 以来就已存在,而 Enums
仅在 Java 5 中出现,JODA Time 被认为是 Calendar
的最佳替代包。
I sometimes get frustrated by the lack of type safety with j.u.Calendar and wondered if there was a good reason for not using enums in the Calendar API that has gone over my head, or if the only reason is that it was probably written before enums were a part of the language and is not worth breaking old code.
A secondary sub-question is: Is it worth my time to create a more type safe wrapper around the Calendar class or has this been done before?
Summary: Calendar
has been around since Java 1.1 whereas Enums
only made it in Java 5, JODA Time is considered the best replacement package for Calendar
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在编写 Calendar 时,枚举还不是 Java 的一部分。由于疏忽、担心破坏向后兼容性、优先级不足、开发人员不同意您的观点,因此没有添加它们 - 选择您的选择。
Enums weren't part of Java when Calendar was written. They haven't been added due to neglect, fear of breaking backward compatibility, insufficient priority, developers who don't agree with you - take your pick.
它是在枚举进入语言之前实现的(枚举到达版本 5)。
JODA Time 被广泛认为是最好的替代包。
It was implemented before enums made their way into the language (enums arrived at version 5).
JODA Time is widely considered the best replacement package.
Calendar
类自 Java 1.1 以来就已存在,这就是它不使用Enums
的原因。我强烈建议使用 Joda 时间 而不是
java.util.Calendar 的火车残骸
。The
Calendar
class has been around since Java 1.1, which is why it doesn't useEnums
.I strongly recommend using Joda time rather than the train-wreck that is
java.util.Calendar
.