如何将日期格式从“dd MMM yyyy”转换为“yyyy-MM-dd”?
我知道我需要使用SimpleDateFormat
,但它并没有起作用,与类似问题的任何解决方案都不起作用。
我有一个日期“2015年12月18日”,我正在尝试进行格式化,但是我得到了这个错误:
java.text.ParseException:无法解析的日期:“2015年12月18日”
这是我的代码:
public String parseDate(String d) {
String result = null;
Date dateObject = null;
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMM yyyy");
try {
dateObject = dateFormatter.parse(d);
dateFormatter.applyPattern("yyyy-MM-dd");
result = dateFormatter.format(dateObject);
} catch (ParseException e) {
System.out.println(e);
}
return result;
}
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMM yyyy", Locale.US);
。 - Alexis C.java.text.ParseException: Unparseable date: "18 Dec 2015"
异常信息。 - Asalas77