[英]Retrieve Oracle Date/Timestamp field as is in Java java.sql.Timestamp without converting to DST
I have column(LOGIN_TIME) in oracle with DATE datatype. 我在DATE数据类型的oracle中有column(LOGIN_TIME)。 I'm trying to read it as Timestamp in Java using JDBC. 我正在尝试使用JDBC将其读取为Java中的时间戳。 One of the rows in my table has a value of 2006-04-02 02:00:01.0 for this column. 该表中的行之一的值为2006-04-02 02:00:01.0。 when I do 当我做
select LOGIN_TIME,CAST(LOGIN_TIME as TIMESTAMP) from TABLE; 从TABLE中选择LOGIN_TIME,CAST(LOGIN_TIME作为TIMESTAMP);
I see that sql-developer shows the value as 我看到sql-developer将值显示为
2006-04-02 02:00:01.0 2006-04-02 02:00:01.0
But when I retrieve this column value in Java using ResultSet.getTimestamp() method, I see the value as 2006-04-02 03:00:01.0. 但是,当我使用ResultSet.getTimestamp()方法在Java中检索此列值时,我看到的值为2006-04-02 03:00:01.0。
After a lot of googling, I understood that 经过大量的搜寻后,我了解到
Now, my questions are 现在,我的问题是
System.out.println(+rs.getTimestamp(index, Calendar.getInstance(TimeZone.getTimeZone("UTC")))); System.out.println(+ rs.getTimestamp(index,Calendar.getInstance(TimeZone.getTimeZone(“ UTC”)))));;
gave me the value as 给我的价值是
2006-04-01 18:00:01.0 2006-04-01 18:00:01.0
Date
in Java does not store the timezone. Java中的Date
不存储时区。 Instead it uses your local timezone to represent values. 而是使用您当地的时区来表示值。 That's why you see different results. 这就是为什么您看到不同的结果的原因。
If you use Java 8 and JDBC 4.2 you may want to use a more reliable java.time.*
package instead. 如果使用Java 8和JDBC 4.2,则可能要使用更可靠的java.time.*
包。 It's simple as this: 就这么简单:
LocalDateTime dateTime = rs.getObject(index, LocalDateTime.class);
If you want to convert LocalDateTime
to java.util.Date
, you can do it this way: 如果要将LocalDateTime
转换为java.util.Date
,可以通过以下方式实现:
Instant instant = dateTime.atZone(ZoneId.systemDefault()).toInstant();
Date dateFromOld = Date.from(instant);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.