0

I am trying to convert a string date format to another date format(dd-MMM-yyyy) using LocalDate.

String date = "2018-04-16";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.US);
LocalDate localDate = LocalDate.parse(date, formatter);

I tried this code with and without the Locale.US in the DateTimeFormatter object. Either way it is returning this exception instead:

java.time.format.DateTimeParseException: Text '2018-04-16' could not be parsed at index 2

Is there a way I can handle this date conversion using LocalDate or should I use SimpleDateFormat?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Arnav Bose
  • 791
  • 4
  • 13
  • 27
  • 2
    Your `String date` is in `yyyy-MM-dd` format but the pattern you have specified is `dd-MMM-yyyy`. Your `date` would have to be like "16-Apr-2018" for this parse to work. – Stephen P Mar 16 '23 at 18:47
  • Ok got the problem now. Actually, my concern is to convert the date to the 16-Apr-2018 format. I have 2018-04-16 as my input data. – Arnav Bose Mar 16 '23 at 18:50
  • how do I convert 2018-04-16 to 16-Apr-2018 ? – Arnav Bose Mar 16 '23 at 18:51
  • 1
    If your input is `yyyy-MM-dd` then your DateTimeFormatter needs to use that same pattern to parse it. _**Then**_ you can output (convert) the resulting `LocalDate localDate` into any format you want. – Stephen P Mar 16 '23 at 18:52
  • *Is there a way I can handle this date conversion using LocalDate …* Yes, certainly, *… or should I use SimpleDateFormat?* No way! That will only risk adding several layers of trouble to your situation. And `SimpleDateFormat` cannot parse into a `LocalDate` (which is what you should want), – Ole V.V. Mar 17 '23 at 15:37
  • You need no formatter for parsing. `LocalDate.parse(date)` parses your string and gives you the `LocalDate` that you want. After that use your formatter for formatting to obtain the desired format: `localDate.format(formatter)` yields `16-Apr-2018`. – Ole V.V. Mar 17 '23 at 15:46
  • 1
    Yes, got it to work now. The problem was I directly tried convert the format to the desired format instead of first converting it to LocalDate and then convert. Now I have updated it and it works as you explained. – Arnav Bose Mar 17 '23 at 15:52
  • 1
    There were two votes to close this question as not reproducible or caused by a typo. It is perfectly reproducible and not caused by any typo. Instead I have closed it as a duplicate, which I consider both more correct and more helpful. – Ole V.V. Mar 17 '23 at 16:25

1 Answers1

3

In your code DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.US); pattern dd-MMM-yyyy is for three letter months like Jun. If you want to parse strings like 2018-04-16, the pattern should be yyyy-MM-dd.

Please refer to the sample code

String date = "2018-04-16";
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate = LocalDate.parse(date, inputFormatter);

Update - For your question in the comment

After you convert String to Local date, the code below should do the trick.

DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.US);
String outputDate = localDate.format(outputFormatter);
Menuka Ishan
  • 5,164
  • 3
  • 50
  • 66