Java date formatting ParseException
I have a string like Friday, August 01, 2014
. I want to format this and show how 2014-08-01
.
I've tried this. but it gavejava.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?
source to share
You need to read the SimpleDateFormat API as explained well.
Please 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, the short or abbreviated form is used. Both forms are accepted for parsing, regardless of the number of pattern letters.
- Number . For formatting, the number of pattern letters is the minimum number of digits, and shorter numbers with a zero addition to that amount. For parsing, the number of pattern letters is ignored unless you want to separate two adjacent fields.
So, for example, MM corresponds to a numeric month, not a monthly name. For the full month name I would use MMMM
, and for the full week name I would use EEEE
. I would use dd
for a two digit date like 01.
eg.
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
source to share