【发布时间】:2017-07-31 10:32:12
【问题描述】:
我有一个带有 Hibernate 的 Spring Boot 应用程序。服务器时区为 UTC。我需要将欧洲/伦敦与 DST(夏令时)一起使用。我在启动时设置:
TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));
以及与 MySQL 的连接
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:mysql://localhost:3306/example?useUnicode=true&characterEncoding=utf8&useSSL=false&useLegacyDatetimeCode=false&useTimezone=true&serverTimezone=Europe/London
name:
username: ****
password: ****
hikari:
data-source-properties:
cachePrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
useServerPrepStmts: true
hibernate.jdbc.time_zone: Europe/London
我不想将时间更改为 UTC。我需要留在夏令时,仅此而已。我的申请仅适用于英国,所以欧洲/伦敦就足够了。
问题是当我有 LocalDateTime 例如:31.07.2017 14:00 并将其存储到数据库时,我在 DB 中有完全相同的条目。但是当我将 LocalTime(单独)存储在 14:00 时,它会在数据库中保存为 13:00。为什么要在幕后转换时间?
更新:
经过更多调试(在 application.properties 中添加了两行):
logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type.descriptor.sql=trace
我看到 Hibernate 不转换时间。它发送了 14:00,但 MySQL 将其转换为 13:00。有什么建议吗?
【问题讨论】:
标签: java mysql hibernate datetime java-time