2014-06-05 321 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语句匹配的时间戳与数据库中
  • 什么到达做一个数据库条目的时间戳匹配您以后收到一个选择