1

I'm trying to turn a String into LocalDate using this code:

String end = sharedPref.getString("endDate", "Not available");
DateTimeFormatter formatter = new DateTimeFormatter("yyyy-MM-dd");
LocalDate endDate = LocalDate.parse(end, formatter);

But it shows the error mentioned in the title. How do I fix it? If there's a better way I'm open to suggestions

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Tashila Pathum
  • 109
  • 2
  • 11

2 Answers2

2

There are no constructor which took a format String, instead you have to call the static method ofPattern like so :

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
2

You don't need an explicit formatter at all in your case.

    LocalDate endDate = LocalDate.parse(end);

Just drop the declaration of formatter. The format that you are trying to parse agrees with ISO 8601. LocalDate parses this format as its default, that is, without the formatter.

For the explanation of what went wrong, see the answer by YCF_L.

Link: ISO 8601.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161