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

parseDefaulting results in a conflict when parsing Hour related fields

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 8, 11, 15
    • Fix Version/s: None
    • Component/s: core-libs

      Description

      ADDITIONAL SYSTEM INFORMATION :
      macos/JDK14

      A DESCRIPTION OF THE PROBLEM :
      Hour can be represented as an ChronoField.CLOCK_HOUR_OF_DAY, CLOCK_HOUR_OF_AMPM, HOUR_OF_AMPM and HOUR_OF_DAY
      all these are resolved to HOUR_OF_DAY during resolving phase (see Parsed.resolveTimeFIelds)

      using a pattern with parseDefaulting leads to a an exception suggesting a conflict being thrown.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
       DateTimeFormatter dateTimeFormatterWithDefault = new DateTimeFormatterBuilder()
                  .appendPattern("dd-MM-uuuu hh:mm a")
                  .parseDefaulting(ChronoField.HOUR_OF_DAY, 23L)
                  .toFormatter();
              TemporalAccessor parsingFails = dateTimeFormatterWithDefault.parse("01-01-2020 01:01 AM");

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      assertEquals(01, parsingFails.get(ChronoField.HOUR_OF_DAY));
      assertEquals(01, parsingFails.get(ChronoField.HOUR_OF_AMPM));
      ACTUAL -
      java.time.format.DateTimeParseException: Text '01-01-2020 01:01 AM' could not be parsed: Conflict found: HourOfDay 23 differs from HourOfDay 1 while resolving AmPmOfDay
      at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2020)
      at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1881)

      ---------- BEGIN SOURCE ----------
      public void testX(){
              DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
                  .appendPattern("dd-MM-uuuu hh:mm a")
                  .toFormatter();
              TemporalAccessor parse = dateTimeFormatter.parse("01-01-2020 01:01 AM");
              assertTrue(parse.isSupported(ChronoField.HOUR_OF_DAY));
              assertTrue(parse.isSupported(ChronoField.HOUR_OF_AMPM));
              assertEquals(01, parse.get(ChronoField.HOUR_OF_DAY));
              assertEquals(01, parse.get(ChronoField.HOUR_OF_AMPM));
              //Parsed.resolveTimeFields() resolves hour field to HOUR_OF_DAY

              DateTimeFormatter dateTimeFormatterWithDefault = new DateTimeFormatterBuilder()
                  .appendPattern("dd-MM-uuuu hh:mm a")
                  .parseDefaulting(ChronoField.HOUR_OF_DAY, 23L)
                  .toFormatter();
              TemporalAccessor parsingFails = dateTimeFormatterWithDefault.parse("01-01-2020 01:01 AM");
              /*

      java.time.format.DateTimeParseException: Text '01-01-2020 01:01 AM' could not be parsed: Conflict found: HourOfDay 23 differs from HourOfDay 1 while resolving AmPmOfDay
      at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2020)
      at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1881)
               */
          }
      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              naoto Naoto Sato
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: