2014-05-15 80 views
0

當我希望得到一個時間爲00:00:00,日曆總是12:00:00日曆不能設置正確小時

Calendar currentDate = Calendar.getInstance(); 
// even set AM_PM it was still 12:00:00 
// currentDate.set(Calendar.AM_PM, Calendar.AM); 
// Calendar.HOUR still not work 
currentDate.set(Calendar.HOUR_OF_DAY, 0); 
currentDate.set(Calendar.MINUTE, 0); 
currentDate.set(Calendar.SECOND, 0); 
currentDate.set(Calendar.MILLISECOND, 0); 

我怎麼能做到嗎?提前致謝。

enter image description here

enter image description here

enter image description here

+0

http://stackoverflow.com/questions/17821601/set-time-to-000000 – Sanjeev

+0

@Sanjeev這就是我想要的。謝謝 – blackdog

+0

@Sanjeev,這是行不通的。我添加了一些圖片來告訴你, – blackdog

回答

1

但是,當是00:00:00,真的,當你到了嗎?今天是?還是昨天?

爲避免混淆,嘗試像

Calendar currentDate = Calendar.getInstance(); 
// even set AM_PM it was still 12:00:00 
// currentDate.set(Calendar.AM_PM, Calendar.AM); 
// Calendar.HOUR still not work 
currentDate.set(Calendar.HOUR_OF_DAY, 0); 
currentDate.set(Calendar.MINUTE, 0); 
currentDate.set(Calendar.SECOND, 1); 
currentDate.set(Calendar.MILLISECOND, 0); 
+0

這很奇怪,我syserr的日期,這是在控制檯00:00:00。但是,我在線上做了一個斷點,然後看着日期,現在是12:00:00。後面我會貼一張照片給你。 – blackdog

0

您可以設置時間是這樣的:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss"); 

Calendar calendar = new GregorianCalendar(2014, 4, 15, 0, 0, 0); 

System.out.println(sdf.format(calendar.getTime())); 

輸出

2014 mai 15 00:00:00 

mai意味着may個月法語。

+0

控制檯輸出的結果是00:00:00。但是當我設置斷點來觀察它的值是12:00:00。你可以在上面的圖片中看到它。或者它只是一個日食中的bug? – blackdog

+0

@blackdog Eclipse以不同的方式格式化小時。 – Stephan