有没有办法用 java.util.date 表示 BC 日期?
是的,我知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单地说,不。 根本没有办法用
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 thatj.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 whichj.u.Date
is a really really bad wrapper, butjava.time.Instant
is the right thing to use if you want that) - their range is a few million years in both directions.