Java SimpleDateFormat:无法解析的日期异常 [英] Java SimpleDateFormat: Unparseable Date exception
本文介绍了Java SimpleDateFormat:无法解析的日期异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码如下所示:
public static void main(String[] args){
Date date = new Date();
DateFormat dateFormat= new SimpleDateFormat("dd-MMM-yyy");
try{
Date formattedDate = dateFormat.parse(date.toString());
System.out.println(formattedDate.toString());
}catch(ParseException parseEx){
parseEx.printStackTrace();
}
}
在上面的代码中, dateFormat .parse(date.toString())
抛出无法解析的日期异常:无法解析日期:1月28日星期一18:53:24 IST 2013
In code above, dateFormat.parse(date.toString())
is throwing unparseable date exception: Unparseable date: "Mon Jan 28 18:53:24 IST 2013
我无法找出原因。
推荐答案
为什么要将日期转换为字符串并将其解析回日期?
Why would you want to convert a date to a string and parse it back to a date?
您的代码失败的原因是您尝试转换完整日期格式化程序只接受dd-MMM-yyy格式的日期。
The reason your code fails is because you are trying to convert a full date with a formatter which only accepts dates in the dd-MMM-yyy-format.
这篇关于Java SimpleDateFormat:无法解析的日期异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文