gpt4 book ai didi

java - getTimestamp() 在 MySQL JDBC 连接器中进行两次时区转换?

转载 作者:可可西里 更新时间:2023-11-01 06:36:01 28 4
gpt4 key购买 nike

我在 MySQL 数据库中有一个 DATETIME 类型的列,其值为 2012-05-07 19:59:12。我正在尝试从数据库中检索此值,假设它存储在 UTC 时区中:

Calendar cal = Calendar.getInstance(new SimpleTimeZone(0, "UTC"));
Date date = resultSet.getTimestamp(1, cal);
System.out.println(date);

它输出(CEST 是我本地的时区,比 UTC 早 2 小时):

Mon May 07 23:59:12 CEST 2012

期望值为:

Mon May 07 21:59:12 CEST 2012

我在 JDBC URL 中使用这三个参数(Europe/BerlinCEST 相同):

..&useGmtMillisForDatetimes=true&useTimezone=true&serverTimezone=Europe/Berlin

是我的缺陷还是应该在JDBC驱动中额外配置什么?我正在使用 mysql:mysql-connector-java:5.1.20

最佳答案

有了这些JDBC URL params它有效:

useGmtMillisForDatetimes=true
useJDBCCompliantTimezoneShift=true
useLegacyDate‌timeCode=false
useTimezone=true
serverTimezone=UTC

关于java - getTimestamp() 在 MySQL JDBC 连接器中进行两次时区转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10488529/

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