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

SimpleDateFormat parse method doesn't parse correctly when lenient mode is on

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      linux_ubuntu

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_161"
      Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
      Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux nikhil-XPS-15-9560 4.13.0-36-generic #40~16.04.1-Ubuntu SMP Fri Feb 16 23:25:58 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      SimpleDateFormat, parse method doesn't throw Parse exception in case of wrong format.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Steps
      1. Create SimpleDateFormat with pattern "dd/MM/yyyy"
      2. setLenient to false
      3. call sdf.parse("16/05/1985R")


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      sdf.parse("16/05/1985R") should throw ParseException
      ACTUAL -
      No Exception thrown

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      //This test will FAIL
      @Test
          public void throwExceptionWhenInvalidDateFormatWithSimpleDateFormat() {
              final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
              sdf.setLenient(true);
                  assertThrows(ParseException.class, () -> sdf.parse("16/05/1985R"));

          }

      //This test will PASS
      @Test
          public void throwExceptionWhenInvalidDateFormatWithSimpleDateFormat() {
              final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
              sdf.setLenient(true);
                  assertThrows(ParseException.class, () -> sdf.parse("R16/05/1985"));

          }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use

      DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
      LocalDate.parse("", formatter);

      This will throw DateTimeParseException

        Attachments

          Activity

            People

            • Assignee:
              nishjain Nishit Jain
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: