我花了几个小时努力了解代码中的 TimeStamp
发生了什么。
Oracle DB 和 java 应用程序都在 PDT 中
从数据库中选择:
select id, time_stamp from some_Table where id = '3de392d69c69434eb907f1c0d2802bf0';
3de392d69c69434eb907f1c0d2802bf0 09-DEC-2014 12.45.41.354000000 PM
select id, time_stamp at time zone 'UTC' from some_Table where id = '3de392d69c69434eb907f1c0d2802bf0';
3de392d69c69434eb907f1c0d2802bf0 09-DEC-2014 12.45.41.354000000 PM
Oracle数据库中的字段是TimeStamp
,因此不存储时区信息。
Timestamp dbTimeStamp = dbRecord.getLastLoginTime();
System.out.println(dbTimeStamp.toString()); // 2014-12-09 12:16:50.365
System.out.println(dbTimeStamp.getTime()); // 1418156210365 --> Tue Dec 09 2014 20:16:50 UTC?
根据documentation , getTime()
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Timestamp object.
为什么要在 getTime()
的响应中添加 8 小时 (PDT - UTC)
额外时间?
最佳答案
TimeStamp.toString()
内部使用 Date.getHours()
,其 javadoc 指出:
Returns the hour represented by this Date object. The returned value is a number (0 through 23) representing the hour within the day that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.
因此,toString
使用的是您的本地时区,而 getDate
则没有。
关于java - Oracle DB 时间戳到 Java 时间戳 : Confusion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27379130/