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?
String dateis inyyyy-MM-ddformat but the pattern you have specified isdd-MMM-yyyy. Yourdatewould have to be like "16-Apr-2018" for this parse to work.yyyy-MM-ddthen your DateTimeFormatter needs to use that same pattern to parse it. Then you can output (convert) the resultingLocalDate localDateinto any format you want.