2014-06-05 216 views
-1

我會得到一個日期,例如Sat May 31 16:38:17 GMT 2014。在DB中,列也具有相同的值。但是當我搜索日期如Sat May 31 16:30:00 GMT 2014它不會搜索。但是,如果我給予比Sat May 31 11:00:00 GMT 2014更少的5:30 hours。它工作正常。日曆/日期時區轉換問題

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT-0:00")); 
cal.setTimeInMillis(inputDate.getTime()); 
cal.set(Calendar.MILLISECOND, 0); 
inputDate = cal.getTime(); 
+0

數據庫使用什麼時區? – Dawnkeeper

+0

當我執行「從雙重選擇dbtimezone」它給了「+00:00」 – user2902067

+0

好的。你到底在做什麼?插入數據庫?從數據庫中選擇?和(如'代碼示例')? – Dawnkeeper

回答

0

底層實現的DateCalendar都使用相同的系統:一個long表示毫秒數自1970年1月,0:00:00北京時間1日。 爲Date(線136 & 168)和HERECalendar(線778)

所以這會讓我們這個見Here

cal.setTimeInMillis(inputDate.getTime()); 
    // this gets the long from the Date and puts it into the Calendar 
    // nothing is changed, you only get added functionality 
    // even if you change the Timezone this only affects how it is displayed 

    cal.set(Calendar.MILLISECOND, 0); 
    // the long is modified by setting the that represents only milliseconds to zero 

    inputDate = cal.getTime(); 
    //the long is stored in a Date and returned 

你得到一個最低限度的改變Date對象。沒有第二條指令就沒有任何改變。

這解釋了爲什麼沒有轉換,但不是爲什麼選擇不起作用。

從您給出的信息看來,數據庫,JVM和系統的三位一體的時區看起來不匹配。

這裏的一些東西:你的系統設置爲IST,你的JVM以某種方式將你的系統時間解釋爲GMT。要檢查這個,你可以運行System.out.println(new Date())。如果它將時區寫出爲GTM,但數字與系統時鐘(IST)相同,那麼這就是您的問題。

你也可以查看:

  • 做一個INSERT語句匹配的時間戳與數據庫中
  • 什麼到達做一個數據庫條目的時間戳匹配您以後收到一個選擇