-1
private SimpleDateFormat dateFormatMonth = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
Date date = dateFormatMonth.parse(strtdate[0]);

strdate[0] contains "2018-06-11"

I'm getting a unparsable exception on this line:

java.text.ParseException: Unparseable date: "2018-06-11"

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Moni
  • 1
  • 1
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jul 05 '18 at 09:52
  • `LocalDate.parse(strtdate[0])`. Since your date string conforms with the ISO 8601 standard, no explicit formatter is needed. So there’ll be no chance of specifying an incorrect format. Also the modern `LocalDate` is a date without time of day, which clearly seems to match your requirements better (a `Date` despite its name is a point in time). – Ole V.V. Jul 05 '18 at 09:58

1 Answers1

2

You are getting the error because you are using the wrong pattern to parse the date. Use this instead:

private SimpleDateFormat dateFormatMonth = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
Date date = dateFormatMonth.parse(strtdate[0]);

Assuming "06" is the month and "11" is day.

Barns
  • 4,850
  • 3
  • 17
  • 31