我有一个日期对象,其日期格式为:2015-09-21 10:42:48:000
我希望它以这种格式显示在 UI 上。2015 年 9 月 21 日 10:42:48
我使用的代码不起作用并抛出此错误:
Unparseable date exception: Unparseable date: "2015-09-21 10:42:48"
这是实际的代码:
String createdOn=f.getCreatedOn().toString();//f.getCreatedOn() returns a date object
SimpleDateFormat format=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Date date=format.parse(createdOn.substring(0,createdOn.length()-3));
log.debug(">>>>>>date now is: "+date);
model.addAttribute("date", date);
model.addAttribute("info", messages);
SimpleDateFormat format1=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
format1.format(date);
log.debug(">>>>>>date now is again: "+date);
最佳答案
Unparseable date exception: Unparseable date: "2015-09-21 10:42:48"
由于您的输入日期格式为yyyy-MM-dd HH:mm:ss
。但您正在尝试使用 dd-MMM-yyyy HH:mm:ss
格式进行解析。
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//change input date format here
Date date=format.parse("2015-09-21 10:42:48:000");
//Date date=format.parse(createdOn);//Here no need of subtracting 000 from your date
SimpleDateFormat format1=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
System.out.println(format1.format(date));
关于Java - 无法解析的日期异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32708035/