为什么 java.util.Calendar 不使用枚举?

发布于 2024-11-16 01:18:50 字数 407 浏览 3 评论 0原文

有时,我会因 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 技术交流群。

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

发布评论

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

评论(3

油焖大侠 2024-11-23 01:18:50

在编写 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.

虫児飞 2024-11-23 01:18:50

它是在枚举进入语言之前实现的(枚举到达版本 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.

空城缀染半城烟沙 2024-11-23 01:18:50

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 use Enums.

I strongly recommend using Joda time rather than the train-wreck that is java.util.Calendar.

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