您的位置:首页 > 编程语言 > Java开发

被Java的SimpleDateFormat打脸了

2017-10-13 14:29 253 查看
String date = "2017-7-12 10:20:9";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(date));

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
at java.text.DateFormat.format(Unknown Source)
at java.text.Format.format(Unknown Source)
at com.ws.actions.StringUtil.main(StringUtil.java:204)
原因竟是:format方法应该传一个Date类型的参数,而不是String 或者是其它的。

String date = "2017-7-12 10:20:9";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//System.out.println(sdf.format(date));

try {
System.out.println(sdf.format(sdf.parse(date)));

} catch (ParseException e) {
e.printStackTrace();
}

被常见的类方法打脸啊/(ㄒoㄒ)/~~。

深入学习参阅:http://www.cnblogs.com/peida/archive/2013/05/31/3070790.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: