Java Date Formatting ParseException

北战南征 提交于 2020-01-24 10:35:09

问题


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:


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");


来源:https://stackoverflow.com/questions/25225318/java-date-formatting-parseexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!