被Java的SimpleDateFormat打脸了

来源:互联网 发布:淘宝商品分类管理 编辑:程序博客网 时间:2024/04/27 21:30

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 Dateat 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

原创粉丝点击