Greg
Greg

Reputation: 43

Java Date Formatting ParseException

I have a String as Friday, August 01, 2014. I want to format this and show as 2014-08-01.

I have tried this. but this gave java.text.ParseException: Unparseable date: "Friday, August 01, 2014"

SimpleDateFormat sdf = new SimpleDateFormat("E, MM d, yyyy");
String dateInString = "Friday, August 01, 2014";
Date date = sdf.parse(dateInString);
System.out.println(date);

How can i do this ?

Upvotes: 3

Views: 190

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You need to read the SimpleDateFormat API as it's all well explained there.

Note this explanation from the API:

  • Text: For formatting, if the number of pattern letters is 4 or more, the full form is used; otherwise a short or abbreviated form is used if available. For parsing, both forms are accepted, independent of the number of pattern letters.
  • Number: For formatting, the number of pattern letters is the minimum number of digits, and shorter numbers are zero-padded to this amount. For parsing, the number of pattern letters is ignored unless it's needed to separate two adjacent fields.

So for instance, MM corresponds to a numeric month, not the month name. For the complete month name, I'd use MMMM, and for the complete week name, I'd use EEEE. I'd use dd for a two digit date such as 01.

e.g.,

SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM dd, yyyy");

Upvotes: 3

Related Questions