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

SimpleDateFormat fails parsing Date if decimal date separators are used

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      generic
    • OS:
      windows

      Description

      ADDITIONAL SYSTEM INFORMATION :
      On Windows. (MacOS, Linux worked fine as far as I remember)
      Fails with Java 8 and Java 11, 12, other versions where not tested.


      A DESCRIPTION OF THE PROBLEM :
      Since we are using the locale provider "HOST" (setting the system property "java.locale.providers"), the method java.text.SimpleDateFormat#parse fails to parse strings (representing valid dates) if the decimal separator and the date separator are equal.
      For example decimal separator is "." and dates are in format "dd.MM.yyyy".
      Without adding "java.locale.providers" it works fine, also if the separators differ (e.g. date format is "dd/MM/yyyy" and decimal separator is"."). And it seems to happen only on Windows.




      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Set the decimal separator to "." on your Windows 10 machine (or the complete locale to something like English(USA)).
      Run the attached source code.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      "Successfully parsed '01.05.2019' to java.util.Date() with SimpleDateFormat#parse (Wed May 01 00:00:00 CEST 2019)"

      ACTUAL -
      "Could not parse string '01.05.2019' to java.util.Date() with SimpleDateFormat#parse"
      java.text.ParseException: Unparseable date: "01.05.2019"
      at java.base/java.text.DateFormat.parse(DateFormat.java:395)
      at de.m.SdfParsingBug.main(SdfParsingBug.java:14)

      ---------- BEGIN SOURCE ----------
      package de.m;

      import java.text.ParseException;
      import java.text.SimpleDateFormat;
      import java.util.Date;

      public class SdfParsingBug {

          public static void main(String[] args) {
              System.setProperty("java.locale.providers", "HOST,COMPAT,SPI");
              SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
              String dateAsString = "01.05.2019";
              try {
                  Date parsedToDate = dateFormat.parse(dateAsString);
                  System.out.println("Successfully parsed '" + dateAsString + "' to java.util.Date() with SimpleDateFormat#parse (" + parsedToDate + ")");
              } catch (ParseException e) {
                  System.out.println("Could not parse string '" + dateAsString + "' to java.util.Date() with SimpleDateFormat#parse");
                  e.printStackTrace();
              }
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Parsing with java.time.LocalDate#parse works fine. Still in some cases it is not possible to change the code to use LocalDate#parse, instead of SimpleDateFormat#parse.

      FREQUENCY : always


        Attachments

          Issue Links

            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: