简体   繁体   English

按Java java.sql.Timestamp中的方式检索Oracle Date / Timestamp字段,而无需转换为DST

[英]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 经过大量的搜寻后,我了解到

  1. Firstly, 2006-04-02 02:00:01.0 is invalid/lost time as after 2006-04-02 01:59:00.0 the clock clicks 2006-04-02 03:00:00.0 due to daylight saving. 首先,由于夏令时,2006-04-02 01:59:00.0之后的2006-04-02 02:00:01.0无效/丢失时间,时钟点击2006-04-02 03:00:00.0。
  2. Oracle Date/Timestamp datatypes don't maintain time zones, hence Oracle accepted this value while inserting into DB without complaint. Oracle Date / Timestamp数据类型不维护时区,因此Oracle在插入数据库时​​毫无保留地接受了该值。
  3. However, since 02:00:01 is an invalid time, Java by default treat it leniently and converted the time to the assumed correct time of 03:00:01. 但是,由于02:00:01是无效时间,因此Java默认会宽大对待它,并将时间转换为假定的正确时间03:00:01。

Now, my questions are 现在,我的问题是

  1. Why did java assume the correct time as 03:00:01? 为什么Java假定正确的时间为03:00:01? Is it dependent on any timezone, if yes, what is the default timezone? 它是否取决于任何时区,如果是,默认时区是什么? I don't think the default timezone here is UTC because 我不认为此处的默认时区是UTC,因为

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

  1. How can I retrieve the value as is from Oracle without DST conversion by JDBC as 2006-04-02 02:00:01.0? 如何在没有通过JDBC进行DST转换的情况下从Oracle检索值,如2006-04-02 02:00:01.0? or get it as 2006-04-02 03:00:01.0 and convert it back to original oracle value of 2006-04-02 02:00:01.0. 或将其获取为2006-04-02 03:00:01.0并将其转换回原始的oracle值2006-04-02 02: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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM