Consider the following snippet:
String dateTimeStr = "-190732550-05-25T15:14:51.136Z";
ZonedDateTime dateTime = ZonedDateTime.parse(dateTimeStr).withZoneSameInstant(ZoneOffset.UTC);
log.info("String: {}", dateTimeStr);
log.info("ZonedDateTime: {}", dateTime);
log.info("EpochMilli: {}", dateTime.toInstant().toEpochMilli());
Output:
String: -190732550-05-25T15:14:51.136Z
ZonedDateTime: -190732550-05-25T15:14:51.136Z
EpochMilli: -6019000079877908864
I expected the above would throw java.time.format.DateTimeParseException.
Edit
"190732550-05-25T15:14:51.136Z" throws java.time.format.DateTimeParseException. Shouldn't be valid too?
tl;dr
This:
… succeeds because your input of a few hundreds of millions of years lies within the range of +/- one billion years in that class.
-1,000,000,000 < -190,732,550 < +1,000,000,000
Use
Instantclass to parse an input string ending inZ, which means an offset from UTC of zero hours-minutes-seconds. No time zone involved, so no need forZonedDateTime.This input will indeed be parsed successfully. You are supplying a year that lies within the range of possible values for that data type.
The minimum value for
Instantis represented in the predefined constantInstant.MIN. To quote the Javadoc:That is the year -1,000,000,000 (one billion).
You input year is only -190,732,550 (hundreds of millions). So your value fits within the accepted range of +/- one billion years.
The
Instantclass is currently implemented as a pair of 64-bitlongintegers. One is for a number of whole seconds since the first moment of 1970 UTC. The other is for a count of nanoseconds in the fractional second. So this class is capable of supporting a wide range of values.Positivity
Note that positive-numbered years with more than four digits must prepend a PLUS SIGN character.
To quote the Javadoc for the predefined constant
DateTimeFormatternamedISO_LOCAL_DATE, which is used byISO_LOCAL_DATE_TIME, which is used byISO_OFFSET_DATE_TIME, which is used byISO_INSTANTwhen callingInstant.parse(emphasis mine):This succeeds:
… whereas this fails:
See this code run live at IdeOne.com.