gpt4 book ai didi

java - 更改jdbc查询连接结果中的时间戳时区

转载 作者:行者123 更新时间:2023-11-30 01:49:03 24 4
gpt4 key购买 nike

我使用 Redshift 和 jdbc。Redshift 服务器 show timezone; 的时区是 UTC。我的应用程序(jvm)时区是+2。

当我通过jdbc执行查询时选择SYSDATE;我得到了正确的时间戳,其值采用 UTC 格式,但不幸的是该值的时区是 +2。

例如:服务器有 2019-06-26T18:10:13.000+0-00 但我得到了 2019-06-26T18:10:13.000+0200

之后,我通过 Jackson 将时间戳映射到 json 并将响应发送给客户端,客户端得到了错误的值 2019-06-26T16:10:13.000+0000。

我无法更改应用程序和映射器的时区。

我该如何解决这个问题?

最佳答案

您需要通过使用 getTimestamp(String columnLabel, Calendar cal) 指定 TimeZone 来指定日期为 UTC。方法:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));

Timestamp ts = resultset.getTimestamp("TimestampColumn", cal);

正如 javadoc 所说:

This method uses the given calendar to construct an appropriate millisecond value for the timestamp if the underlying database does not store timezone information.

关于java - 更改jdbc查询连接结果中的时间戳时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56778837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com