1

I'm trying to parse a date string using Java8 LocalDateTime without any success.

The exception is: DateTimeParseException: Text '28-APR-2015 01:25:00 PM' could not be parsed at index 3

The string to parse is: 28-APR-2015 01:25:00 PM and the pattern I'm currently using is dd-LLL-yyyy hh:mm:ss a

Where am I wrong?

Thank you

Adding example code:

String text = "28-APR-2015 01:25:00 PM";
DateTimeFormatter fromatter = DateTimeFormatter.ofPattern("dd-LLL-yyyy hh:mm:ss a");
try {
        String out = LocalDateTime.parse(text, formatter)
        System.out.println(out);
    } catch (DateTimeParseException e) {
        e.printStackTrace();
    }

Solved using the response from @Florin using:

    DateTimeFormatter tertiaryFormatter = new DateTimeFormatterBuilder()
        .parseCaseInsensitive().parseLenient().appendPattern("dd-LLL-yyyy hh:mm:ss a").toFormatter();
Marco Fedele
  • 2,090
  • 2
  • 25
  • 45
  • 2
    Can you include a short but complete example demonstrating the problem? – Jon Skeet Jan 12 '16 at 18:05
  • 2
    I've managed to reproduce this; I think you want `MMM` rather than `LLL` (although the docs aren't clear on what `L` means); but that's case-sensitive so you'd still need `Apr` rather than `APR`. Still working on how to make it case-insensitive... – Jon Skeet Jan 12 '16 at 18:11
  • What does it mean "without any success"? Do you have any error? – Dzianis Yafimau Jan 12 '16 at 18:22
  • 1
    You could create a more permissive DateTimeFormatter class instead of relying on the defaults. For instance: DateTimeFormatter formatter = new DateTimeFormatterBuilder() .parseCaseInsensitive() .parseLenient() .appendPattern("dd-LLL-yyyy hh:mm:ss a") .toFormatter() – Florin Jan 12 '16 at 18:46
  • Possible duplicate of [DateTimeFormatter month pattern letter "L" fails](http://stackoverflow.com/questions/30518954/datetimeformatter-month-pattern-letter-l-fails) – ctomek Jan 12 '16 at 18:49

2 Answers2

1

MMM in English will be Apr

LLL in English will be 4

Please run this snippet - it will explain you a lot:

    asList("MMM", "LLL").forEach(ptrn
            -> System.out.println(ptrn + ": " + ofPattern(ptrn, Locale.ENGLISH).format(Month.APRIL))
    );
    Locale.setDefault(Locale.ENGLISH);
    String textM = "28-Apr-2015 01:25:00 PM";
    DateTimeFormatter formatterM = DateTimeFormatter.ofPattern("dd-MMM-yyyy hh:mm:ss a");
    System.out.println(LocalDateTime.parse(textM, formatterM));

    String textL = "28-4-2015 01:25:00 PM";
    DateTimeFormatter formatterL = DateTimeFormatter.ofPattern("dd-LLL-yyyy hh:mm:ss a");
    System.out.println(LocalDateTime.parse(textL, formatterL));

If you need to use APR then you need to build your own DateTimeFormatter using DateTimeFormatterBuilder and set parseCaseInsensitive option.

zacheusz
  • 8,750
  • 3
  • 36
  • 60
0

The problem is because of your time pattern issue. you need make sure your time format could matching with the pattern.

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

enter image description here

SharpLu
  • 1,136
  • 2
  • 12
  • 28