Home > Software engineering >  Is there a way to represent BC dates with java.util.date?
Is there a way to represent BC dates with java.util.date?

Time:03-28

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?

CodePudding user response:

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.

  • Related