I'm trying to parse a really large date (but still way less than Instant.MAX) using the Instant.parse method but getting an error.
String input = "78000000-01-01T00:00:00Z";
Instant instant = Instant.parse(input);
Exception:
Exception in thread "main" java.time.format.DateTimeParseException: Text '78000000-01-01T00:00:00Z' could not be parsed at index 0
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2106)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:2008)
at java.base/java.time.Instant.parse(Instant.java:399)
The BC date "-78000000-01-01T00:00:00Z" is parsed ok.
Did I find a bug in Java :)?
Feature, not a bug
No, it is not a bug - is working as specified!
From documentation of
parse():and from ISO_INSTANT
ISO_INSTANTrefers toISO_OFFSET_DATE_TIMEtoISO_LOCAL_DATE_TIMEtoISO_LOCAL_DATE. The last one says:This is meant to imply that years to be parsed must abide by the mentioned format too.
Finally, from Wikipedia for ISO-8601 (Years):
Emphasis mine
The input string
"78000000-01-01T00:00:00Z"is missing that+sign, while"-78000000-01-01T00:00:00Z"correctly starts with a-sign.The input
"+78000000-01-01T00:00:00Z"is parsed without error.Hint: you can check the format by formatting a test date/time/... before parsing.
For example:
Output: