0

This piece of code is working fine on my old computer. However, once I moved it to this computer , i received this exception. I looked into the possible answers from other posts, i.e. adding Locale.US do not work.

Basically,

        Locale locale = new Locale("en", "US");
        DateTimeFormatter dtformatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS",locale);
        LocalTime time = LocalTime.parse("20190502050634678",dtformatter);

Exception in thread "main" java.time.format.DateTimeParseException: Text '20190502050634678' could not be parsed at index 0
    at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
    at java.time.format.DateTimeFormatter.parse(Unknown Source)
    at java.time.LocalTime.parse(Unknown Source)

I am not sure how do I overcome this ? I removed Locale and that do not work either.

user1769197
  • 2,132
  • 5
  • 18
  • 32

1 Answers1

2

This is because Java failed to parse the fraction of a second part. There is a bug in the jdk thats not fixed until Java 9. Probably that's why it fails when you move the program from one to another computer, because they used different Java runtime. You can update the runtime on your other computer to Java 9.

Or use the following code.

    Locale locale = new Locale("en", "US");
    DateTimeFormatter formatter =
            new DateTimeFormatterBuilder()
                    .appendPattern("yyyyMMddHHmmss")
                    .appendValue(ChronoField.MILLI_OF_SECOND, 3)
                    .toFormatter();
    LocalTime time = LocalTime.parse("20190502050634678",formatter);

Solution is borrowed from here Is java.time failing to parse fraction-of-second? .

HarryQ
  • 1,281
  • 2
  • 10
  • 25