java - Oracle DB 时间戳到 Java 时间戳 : Confusion

标签 java oracle timestamp gmt

我花了几个小时努力了解代码中的 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/

相关文章:

java - 提取 "="之后和 "&"之前的内容,java中的正则表达式

java - 安装另一个Java JDK后找不到Maven

java - 是否可以使用 Hibernate 连接到 oracle 12c 可插拔数据库?

oracle - Entity Framework 创建疯狂的 bin 表?为什么?

java - 如何将 Joda-Time 与 java.sql.Timestamp 一起使用

r - 对象创建时间戳

java - Sonarqube, "String contains no format specifiers"记录常量字符串消息时

sql - 正则表达式中的多种模式

Python Pandas 在 read_csv 中结合时间戳列和 fillna

java - 从外部类路径/jar 加载 Freemarker 模板