我在 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/Berlin
与 CEST
相同):
..&useGmtMillisForDatetimes=true&useTimezone=true&serverTimezone=Europe/Berlin
是我的缺陷还是应该在JDBC驱动中额外配置什么?我正在使用 mysql:mysql-connector-java:5.1.20
。
最佳答案
有了这些JDBC URL params它有效:
useGmtMillisForDatetimes=true
useJDBCCompliantTimezoneShift=true
useLegacyDatetimeCode=false
useTimezone=true
serverTimezone=UTC
关于java - getTimestamp() 在 MySQL JDBC 连接器中进行两次时区转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10488529/