Search code examples
javaformatterlocaldate

How to convert string 2019-03-29T10:45-05:00[America/Chicago] to localdate?


selectedStart = "2019-03-29T10:45-05:00[America/Chicago]";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate parsedDate = LocalDate.parse(selectedStart, formatter);

I get my selectedStart string from a tableview row and try converting it to LocalDate but I get the error :

Text '2019-03-29T10:45-05:00[America/Chicago]' could not be parsed, unparsed text found at index 10

I ONLY want the date in yyyy-MM-dd format do NOT want to get minutes, seconds, timezone etc...


Solution

  • This should be sufficient. Note that I used the full date pattern for the first transformation, which is needed (parse would throw a DateTimeParseException).

    final String selectedStart = "2019-03-29T10:45-05:00[America/Chicago]";
    final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mmXXX'['VV']'");
    final LocalDate parsedDate = LocalDate.parse(selectedStart, formatter);