3

I encountered an issue where DateTimeFormatter.ISO_DATE_TIME where date cannot be parsed if year is more then 9999. I was looking for a bug report or to understand what I am doing wrong. Also - JodaTime parse works for the same string. Java version - 8. This test fails:

    public void testParseFarInTheFuture()
    {
        String str = "10000-04-11T01:24:55.887-03:56";

        DateTimeFormatter.ISO_DATE_TIME.parse(str);
    }

Same test with year 9999 works.

Am I doing something wrong or is there a bug?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Heisenberg
  • 3,153
  • 3
  • 27
  • 55
  • 4
    Does this answer your question? [Elastic Search and Y10k (years with more than 4 digits)](https://stackoverflow.com/questions/62541394/elastic-search-and-y10k-years-with-more-than-4-digits). In other words, years containing more than 4 digits need a `+` or `-` at the start of the string. – andrewJames Apr 21 '22 at 17:47
  • 1
    See similar Question, [*Why "-190732550-MM-ddTHH:mm:ss.SSSZ" datetime does not fail to parse*](https://stackoverflow.com/q/71886611/642706). – Basil Bourque Apr 21 '22 at 17:58

1 Answers1

7

Prepend +

  • Years with more than four digits are expected to have a leading PLUS SIGN character (+) for positive years (AD).
  • Negative years (BC) with any count of integers always lead with a MINUS SIGN (-).

The java.time classes generally follow the rules laid down in the ISO 8601 standard. See Wikipedia page on handling years in ISO 8601.

Code:

OffsetDateTime ldt = OffsetDateTime.parse( "+10000-04-11T01:24:55.887-03:56" ) ;

See this code run live at IdeOne.com.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154