ParseException when parsing a date of yyyy-MM-dd format

1.5k Views Asked by At
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"

1

There are 1 best solutions below

0
On

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.