Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8327359

Unexpected behaviour in parsing the year if formatter pattern has optional field

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      > uname -a
      Linux XXX 6.5.0-18-generic #18~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Feb 7 11:40:03 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

      > java --version
      openjdk 21.0.2 2024-01-16
      OpenJDK Runtime Environment (build 21.0.2+13-Ubuntu-122.04.1)
      OpenJDK 64-Bit Server VM (build 21.0.2+13-Ubuntu-122.04.1, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      Parsing of the year is unexpected if there is optional fields in the pattern e.g. "uuuu[MM]". This pattern cannot parse input: "200001". It fails on parsing the year part. Somehow year parsing tries to greedily parse the whole input and fails.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      jshell> import java.time.format.DateTimeFormatterBuilder;
      jshell> (new DateTimeFormatterBuilder()).appendPattern("uuuu[MM]").toFormatter().parse("200001");


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      {MonthOfYear=1, Year=2000},ISO
      ACTUAL -
      | Exception java.time.format.DateTimeParseException: Text '200001' could not be parsed at index 0
      | at DateTimeFormatter.parseResolved0 (DateTimeFormatter.java:2108)
      | at DateTimeFormatter.parse (DateTimeFormatter.java:1936)
      | at (#43:1)

      ---------- BEGIN SOURCE ----------
      jshell> import java.time.format.DateTimeFormatterBuilder;
      jshell> (new DateTimeFormatterBuilder()).appendPattern("uuuu[MM]").toFormatter().parse("200001");
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      jshell> (new DateTimeFormatterBuilder()).appendValue(ChronoField.YEAR, 4, 4, SignStyle.EXCEEDS_PAD).appendPattern("[MM]").toFormatter().parse("200001");
      $47 ==> {MonthOfYear=1, Year=2000},ISO

      FREQUENCY : always


            naoto Naoto Sato
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: