2012-02-18 73 views
1

我的代碼:mysql的日期時間日曆

Calendar c = Calendar.getInstance();  
c.setTime(res.getDate("my_date")); //res is ResultSet 

System.out.println(c.getTime()); 
System.out.println(res.getString("my_date")); 

輸出:

Thu Oct 11 00:00:00 IST 2012 
2012-10-11 02:50:00.0 

在日曆中,不考慮時間的一部分。我使用了日曆,因爲我必須做一些比較。

爲什麼時間部分被省略?

回答

2

你應該使用類似:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
out.println(sdf.format(rs.getTimestamp("your_date_field").getTime())); 

順便說一句,你必須創建一個適合你的SQL日期格式構造SimpleDateFormat。

+0

輸出:「2012-10-11 24:00:00」。仍然是時間部分不起作用。 – 2012-02-18 11:00:49

+0

哎呀看到我的編輯,你必須使用getTimeStamp,getDate只返回日期(沒有時間)的一部分。 – 2012-02-18 11:13:20