3

I am attempting to use the Java 8 DateTimeFormatter class to parse a time string that is in Spanish. After several hours of searching, I cannot find any hints. The error being returned is:

java.time.format.DateTimeParseException: Text '905 PM AST MARTES 7 AGOSTO 2018' could not be parsed at index 4

This seem to be the start of the 'PM' element.

Here is the relevant code:

DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
builder.parseCaseInsensitive();
builder.appendPattern("hmm a z EEE d MMM yyyy");
DateTimeFormatter formatter = builder.toFormatter(new Locale("es", "PR"));

String spanishDate = "905 PM AST MARTES 7 AGOSTO 2018"
Instant issuanceInstant = Instant.from(formatter.parse(spanishDate));

I have tried formatting it differently, but this is a standard format used on meteorological messages that I need to parse. Any help would be greatly appreciated.

The Impaler
  • 45,731
  • 9
  • 39
  • 76
Matt Davis
  • 31
  • 2
  • 1
    FYI, the builder methods are chainable. You don't need a separate statement for each one. – shmosel Aug 08 '18 at 20:14
  • Did you check what the am/pm symbols are in your Locale? It's possible they're something other than AM and PM, in which case PM might not be understood. – Dawood ibn Kareem Aug 08 '18 at 21:08

2 Answers2

3

EEE and MMM are short form. You need to use 4 letters for full form:

hmm a z EEEE d MMMM yyyy
shmosel
  • 49,289
  • 6
  • 73
  • 138
  • Thank you for that suggestion. It still has the same error, but once that's solved, this would have probably been the next one. – Matt Davis Aug 08 '18 at 20:10
  • I wonder if there is something specific to a machine involved? I'm just using the standard Java 8 libraries. – Matt Davis Aug 08 '18 at 20:14
  • @MattDavis What version? – shmosel Aug 08 '18 at 20:18
  • Interestingly, if I remove the PM, change to 24 hour time, and remove the 'a' from you suggested format String, it works. builder.appendPattern("HHmm z EEEE d MMMM yyyy"); String spanishDate = "2105 AST MARTES 7 AGOSTO 2018"; – Matt Davis Aug 08 '18 at 20:19
  • It appears I misspoke. The machine has been updated, and it's using the Java 10 libraries. jre-10.0.2 – Matt Davis Aug 08 '18 at 20:21
0

After reverting to Java 8, the bug has disappeared. Thanks for the help everyone! I'll file a report against it.

Matt Davis
  • 31
  • 2