Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to represent BC dates with java.util.date?

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading