当我在代码下面运行时出现Unparseable错误。如何将dd MMM yyyy格式转换为dd / MM / yyyy格式?
public Calendar myMethod(){
String dateStr = "16 Dec 2014"
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date thedate = formatter.parse(dateStr);
Calendar cal = Calendar.getInstance();
cal.setTime(thedate);
return cal;
}
答案 0 :(得分:6)
假设您修复了明显的语法错误,那么:
String dateStr = "16 Dec 2014" // <== This date is in dd MMM yyyy
formatter = new SimpleDateFormat(dd/MM/yyyy); // <== This SDF is in dd/MM/yyyy
Date thedate = formatter.parse(dateStr); // <== So how can it be expected to parse?
您所做的是为您需要解析的格式创建解析器,并为您想要的格式创建格式化程序:
String dateStr = "16 Dec 2014";
SimpleDateFormat parser = new SimpleDateFormat("dd MMM yyyy");
Date thedate = parser.parse(dateStr);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateStrReformatted = formatter.format(thedate);