我有一个字符串 Friday, August 01, 2014
。我想对其进行格式化并显示为 2014-08-01
。
这个我试过了。但这给出了 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);
我该怎么做?
最佳答案
您需要阅读 SimpleDateFormat API因为那里都有很好的解释。
请注意 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.
例如,MM 对应于数字月份,而不是月份名称。对于完整的月份名称,我将使用 MMMM
,对于完整的周名称,我将使用 EEEE
。我会使用 dd
作为两位数的日期,例如 01。
例如,
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
关于Java 日期格式化 ParseException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25225318/