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

SimpleDateFormat is not able to parse if a numeric literal follows a field

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 8, 11, 12, 13
    • Fix Version/s: None
    • Component/s: core-libs
    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      A DESCRIPTION OF THE PROBLEM :
      If you have a numeric literal following a field in a date expression, the parser logic is erroneous and the numeric literal is parsed as part of the field.

      The erroneous code is this in SimpleDateFormat#parse:

      if (!(nextTag == TAG_QUOTE_ASCII_CHAR ||
            nextTag == TAG_QUOTE_CHARS)) {
          obeyCount = true;
      }

      It only checks whether the next tag is a non-literal tag and if so ("HHmm") parses the exact amount of characters in the field (2).
      If there is no field but a literal as the next tag like ("HH'hours'" or "HH:mm") the parser reads as far as it finds digits and makes this the field value which obviously goes havoc if you have something like "HH00" where you then get 1300 hours instead of 13 hours and two literal zeroes.

      The parser either has to also obey the count if the next literal tag starts with a digit or at least try to back off like the regex engine to try to fulfill the pattern.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      parsed correctly
      ACTUAL -
      "ParseException: Unparseable date: ..."

      ---------- BEGIN SOURCE ----------
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS'000000'");
      System.out.println(sdf.parse(sdf.format(new Date())));

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS000000");
      System.out.println(sdf.parse(sdf.format(new Date())));

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Insert some non-numeric character at the appropriate place in the input text and add it to the format like

      SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS000000");
      SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS 000000");
      String dateString = sdf1.format(new Date());
      System.out.println(sdf2.parse(dateString.replaceFirst("000000$", " 000000")));


      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: