底层实现的Date
和Calendar
都使用相同的系统:一个long
表示毫秒数自1970年1月,0:00:00北京时间1日。 为Date
(线136 & 168)和HERE为Calendar
(线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语句匹配的时间戳与数据库中
- 什么到达做一个数据库条目的时间戳匹配您以后收到一个选择
数据库使用什么时区? – Dawnkeeper
当我执行“从双重选择dbtimezone”它给了“+00:00” – user2902067
好的。你到底在做什么?插入数据库?从数据库中选择?和(如'代码示例')? – Dawnkeeper