3

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 ?

1 Answer 1

3

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");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.