I am getting multiple dates from UI which I store in list. I want to sort these dates
List<String> li = new ArrayList<>();
li.add("11/1/2022 2:59 am");
li.add("10/21/2022 9:33 pm"); li.add("10/20/2022 8:26 am");
and so on
I am trying to sort with following code
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm a", Locale.US);
Collections.sort(li, (s1, s2) -> LocalDateTime.parse(s1, formatter).
compareTo(LocalDateTime.parse(s2, formatter)));
but getting error
"Exception in thread "main" java.time.format.DateTimeParseException: Text '10/21/2022 9:33 pm' could not be parsed at index 11"
The accepted solution at How to sort Date which is in string format in java? is not working for me. Could anyone please help here?