Java 从数据库读取时间戳比GMT时间晚4小时。但存储为GMT
我的代码说明了一切。这个问题很简单,但我似乎不知道如何正确读回这个时间戳:Java 从数据库读取时间戳比GMT时间晚4小时。但存储为GMT,java,database,timestamp,Java,Database,Timestamp,我的代码说明了一切。这个问题很简单,但我似乎不知道如何正确读回这个时间戳: Calendar now = Calendar.getInstance(TimeZone.getTimeZone("GMT")); //later, this code gets executed, and the "now" calendar object is the value: insert into mytable (mydate) values(?) 当我读这张表时,我得到了一个时间戳。为什么时间戳是4小
Calendar now = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
//later, this code gets executed, and the "now" calendar object is the value:
insert into mytable (mydate) values(?)
当我读这张表时,我得到了一个时间戳。为什么时间戳是4小时后
当然,这是从我住的地方到格林尼治标准时间的偏移量,但时间戳只是一个数字。我想,由于我的日历位于GMT区域,所以它的时间戳被插入为GMT时间
Timestamp mydate = rs.getTimestamp("mydate");
我看到时间戳包装了Date对象。在我从数据库读取日期对象后,它包装的日期对象被划分为我的时区(不是GMT)。我不知道如何将其转换回GMT,或者是否有办法将其直接读取为GMT。找到了我需要的:
java.util.Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
Timestamp mydate = rs.getTimestamp("mydate", cal);
您没有提供如何读取时间戳的代码,我猜当您阅读时,它会转换为您的时区Hi Tima,我更新了问题,谢谢。