有没有办法用 java.util.date 表示 BC 日期?

发布于 2025-01-17 04:57:44 字数 78 浏览 1 评论 0原文

是的,我知道 java.util.Date 已经非常过时了。话虽如此,有没有办法使用此类来表示 1 CE 之前的日期,或者我必须迁移到其他类?

Yes, I understand that java.util.Date is extremely outdated. That being said, is there a way to represent dates before 1 CE using this class, or must I migrate to a different class?

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

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

发布评论

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

评论(1

一世旳自豪 2025-01-24 04:57:44

简单地说,不。 根本没有办法用java.util.Date来表示日期。任何日期。

juDate 是一个用词不当,并且是您不应该使用的过时 API。它实际上代表时间上的一个瞬间,与时区无关,因此与年、月和日的概念无关。这就是为什么 .getYear() 被弃用,以及为什么 juDate 拥有的唯一字段是包含 epochmillis 的 long 字段。

您必须迁移到 java.time。具体来说, java.time.LocalDate

请注意,一旦将它们放入时区,您就可以表示 1CE 之前的时刻。 epochmillis-in-a-long(这就是 System.currentTimeMillis() 所代表的,并且 juDate 是一个非常糟糕的包装器,但是 java.time如果您想要的话,.Instant 是正确的选择) - 它们的范围在两个方向上都是几百万年。

Trivially, no. There is no way to represent dates with java.util.Date at all. Any date.

j.u.Date is a misnomer, and is obsolete API you should not be using. It actually represents an instant in time, unconnected to a timezone, and therefore, unconnected to the concept of years, months, and days. That's why e.g. .getYear() is deprecated, and why the only field that j.u.Date has is a long containing epochmillis.

You must migrate to java.time. Specifically, java.time.LocalDate.

Note that you can represent moments in time that, once you place them in a timezone, are before 1CE just fine. epochmillis-in-a-long (which is what System.currentTimeMillis() represents, and for which j.u.Date is a really really bad wrapper, but java.time.Instant is the right thing to use if you want that) - their range is a few million years in both directions.

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