被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
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
相关文章推荐
- Java中时间相关函数Date、Calendar、SimpleDateFormat
- Java多线程(三)SimpleDateFormat
- 深入理解Java:SimpleDateFormat安全的时间格式化
- Java Date SimpleDateFormat
- Java常用类库之时间操作类——Date、Calendar、DateFormat、SimpleDateFormat及实例操作
- java日期处理SimpleDateFormat等
- Java SimpleDateFormat使用
- java中设置日期格式的方式一般用SimpleDateFormat("YY-MM-dd")就可以了,但为什么有的 SimpleDateFormat("YY-MM-dd",locale.getDefau
- java日期格式大全 format SimpleDateFormat
- java日期格式大全 format SimpleDateFormat
- 深入理解Java:SimpleDateFormat安全的时间格式化
- 深入理解Java:SimpleDateFormat安全的时间格式化
- java SimpleDateFormat
- java常用类库续1(日期操作类Date、Calendar、DateFormat、SimpleDateFormat、Math与Random类)
- java时间格式化----java.text.SimpleDateFormat
- Java中SimpleDateFormat用法详解
- java获取当前时间后,经SimpleDateFormat格式化成String类型,差8小时
- JAVA SimpleDateFormat 中英文时间格式化转换
- java.text.SimpleDateFormat使用介绍
- java日期类操作:Date、SimpleDateFormat、Calendar简介