-1

Input Text is 20FEB2020

The following code block throws a DateTimeParseException with the message Text '28Feb2020' could not be parsed, unparsed text found at index 7:

String issueDate = abcIssueDate.substring(0, 3)
                  + abcIssueDate.substring(3).toLowerCase();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMMyy", Locale.US);
LocalDate localDate = LocalDate.parse(issueDate, formatter);
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
vilosh
  • 9
  • 1
  • 2
    `yy` should be `yyyy`? – Robby Cornelissen Nov 02 '21 at 09:29
  • Not answering your question, rather than fixing the case of `Feb` by hand, use a `DateTImeFormatterBuilder` to specify case insensitive parsing. – Ole V.V. Nov 02 '21 at 09:43
  • 1
    I am not sure where you are stuck here. The pattern clearly does not match the input, which is very easy to spot at the very first glance. – f1sh Nov 02 '21 at 09:46
  • 1
    Does this answer your question? [Java 8 DateTimeFormatter two digit year 18 parsed to 0018 instead of 2018?](https://stackoverflow.com/questions/48156022/java-8-datetimeformatter-two-digit-year-18-parsed-to-0018-instead-of-2018) Not the exact same question, but the answers could be helpful to you too. – Ole V.V. Nov 02 '21 at 09:51
  • Welcome to Stack Overflow. I upvoted because I found the question and the problem clear and very answerable. My guess is that the downvotes are from users who found the question poorly searched and researched. [Guideline #1 on Stack Overflow](https://stackoverflow.com/help/how-to-ask) says search and research before posting your question. – Ole V.V. Nov 03 '21 at 04:58

1 Answers1

2

The exception your code block throws is very likely to be caused by the pattern of your DateTimeFormatter. As already commented below your question, you are using two y for a year that has 4 digits.
So you could change the pattern to "ddMMMyyyy", which might work.

Also, I strongly recommend you to build and use a DateTimeFormatter with DateTimeFormatterBuilder#parseCaseInsensitive that parses the input string case-insensitively:

public static void main(String[] args) throws IOException {
    String time = "20FEB2020";
    // build a DateTimeFormatter that parses case-insensitively
    DateTimeFormatter dtf = new DateTimeFormatterBuilder()
                                    .parseCaseInsensitive()
                                    .appendPattern("ddMMMuuuu")
                                    .toFormatter(Locale.ENGLISH);
    LocalDate localDate = LocalDate.parse(time, dtf);
    System.out.println(localDate);
}

The result is (implicitly using the toString() method of LocalDate):

2020-02-20
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 1
    In particular upvoted for the demonstration of `.parseCaseInsensitive()`. It gives much nicer code. – Ole V.V. Nov 02 '21 at 10:03