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

DateTimeFormatter.ofPattern("yyyyMMdd'000000+0000'") can be formatted but not parsed

XMLWordPrintable

    • x86_64
    • windows_10

      A DESCRIPTION OF THE PROBLEM :
      The usage of the pattern "yyyyMMdd'000000+0000'" does not work as expected.
      It can successfully format an existing date but throws a DateTimeParseException when trying to parse a correct string

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyyMMdd'000000+0000'");
      String myDateString = LocalDate.of(2019, 1, 1).format(f);
      // Successfully displays 20190101000000+0000
      System.out.println(myDateString);

      // Throws DateTimeParseException
      LocalDate myLocalDate = LocalDate.parse(myDateString, f);
      System.out.println(myLocalDate);

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The date is parsed correctly and displays 2019-01-01 (locale dependent)
      ACTUAL -
      java.time.format.DateTimeParseException: Text '20190101000000+0000' could not be parsed at index 0

      ---------- BEGIN SOURCE ----------
      package ch.fer.mep.domain;

      import java.time.LocalDate;
      import java.time.format.DateTimeFormatter;

      public class BugReport {

      public static void main(String... args) {
      DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyyMMdd'000000+0000'");
      String myDateString = LocalDate.of(2019, 1, 1).format(f);
      System.out.println(myDateString);

      LocalDate myLocalDate = LocalDate.parse(myDateString, f);
      System.out.println(myLocalDate);
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use a shorter pattern like "yyyyMMdd" and parse only the first 8 characters of the string...
      DateTimeFormatter f2 = DateTimeFormatter.ofPattern("yyyyMMdd");
      LocalDate myLocalDate = LocalDate.parse(myDateString.substring(0, 8), f2);

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: