java.sql.Timestamp的格式String轉換

 用Timestamp來記錄日期時間還是很方便的,但有時候顯示的時候是不需要小數位後面的毫秒的,這樣就需要在轉換爲String時重新定義格式。

Timestamp轉化爲String:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定義格式,不顯示毫秒 

Timestamp now = new Timestamp(System.currentTimeMillis());//獲取系統當前時間 

String str = df.format(now);

 

String轉化爲Timestamp:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = df.format(new Date());

Timestamp ts = Timestamp.valueOf(time);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章