gpt4 book ai didi

java - 将 Java 连接到 PostgreSQL 时强制 TZ?

转载 作者:行者123 更新时间:2023-12-02 00:10:39 27 4
gpt4 key购买 nike

我有两个 Java 应用程序通过 Hibernate 和 JPA 连接到 PostgreSQL 数据库。有没有办法让他们的连接模仿 set timezone 命令,以便在连接期间,时区的设置与数据库的默认时区不同?

最佳答案

自从commit 9d6b7930e6从2011年开始,JDBC驱动程序从JVM默认时区设置 session 时区。请参阅 java.util.TimeZone.getDefault() 的文档有关如何确定的详细信息:

  • Use the user.timezone property value as the default time zone ID if it's available.
  • Detect the platform time zone ID. The source of the platform time zone and ID mapping may vary with implementation.
  • Use GMT as the last resort if the given or detected time zone ID is unknown.

所以最简单的方法是在启动 JVM 时设置 user.timezone 属性:

java -Duser.timezone=UTC -cp ... MyProgram

关于java - 将 Java 连接到 PostgreSQL 时强制 TZ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58123614/

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