這個小錯挺有意思的,記錄一下
導出Excel的時候,同事直接用 format ……
前提:數據庫中該字段是 Timestamp ---- 2016-06-20 22:49:02.967
寫個測試說明一下:
String temp = "2016-06-20 22:49:02.967"; //temp = temp.substring(0,aaa.lastIndexOf(".")); Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(temp); String str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); System.out.println("==== this time ==="+str);
這個 format 和 parse 需要傳入的參數類型是有要求的,不能亂傳。
其實也可以直接在sql中直接處理。to_date(ttvl.use_date, 'yyyy-mm-dd hh24:mi:ss') --看需要格式
亂傳……
那就報錯唄……
java.lang.IllegalArgumentException: Cannot format given Object as a Date at java.text.DateFormat.format(DateFormat.java:301) at java.text.Format.format(Format.java:157) at com.zhaopin.QueryTest.main(QueryTest.java:32)