对于Spring项目,mysql-connector-java
已经从6.0.6
迁移到8.0.11
。
因此,使用8.0.11
时,问题如下:
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException:
The server time zone value 'PET' is unrecognized or represents more than one time zone.
You must configure either the server or JDBC driver (via the serverTimezone configuration property)
to use a more specifc time zone value if you want to utilize time zone support.
在做完研究之后
解决方案是更改URL
(我不想返回到以前的版本)
来自:mysql.jdbcUrl = jdbc:mysql://localhost:3306/web_v01?useSSL=false
mysql.jdbcUrl = jdbc:mysql://localhost:3306/web_v01?useSSL=false&serverTimezone=UTC
的
观察添加的&serverTimezone=UTC
在我的数据库中,我有以下内容:
mysql> select * from persona;
+-----+--------------+-------------+------------+
| id | nombre | apellido | fecha |
+-----+--------------+-------------+------------+
...
| 088 | Something | Something | 1981-07-06 |
...
+-----+--------------+-------------+------------+
当Spring
应用程序通过RowMapper<Persona>
从数据库中进行检索时,我可以确认rs.getDate("fecha")
返回1981-07-05
(观察到日期已经减少了一天,这是not正确的)
如果mysql-connector-java
返回到6.0.6
,从而返回mysql.jdbcUrl = jdbc:mysql://localhost:3306/web_v01?useSSL=false
(no serverTimezone=UTC
),则rs.getDate("fecha")
返回1981-07-06
(预期的结果)
那么,如何在8.0.11
中解决这个问题呢?
当serverTimezone
从一开始就没有被声明时,我想要有相同的行为,当然也避免了异常。
因此,如果考虑到声明serverTimezone
的值并不重要,那么解决方案会更好。
发布于 2019-07-24 11:12:51
试着使用
jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=CST
发布于 2018-08-20 08:59:04
有几个与时区相关的属性:
useTimezone:在客户端和服务器时区之间转换时间/日期类型(true/false,默认为'false')?这是遗留日期-时间代码的一部分,因此该属性仅在“useLegacyDatetimeCode=true”时有效。默认值: false
useLegacyDatetimeCode:在结果集和语句中使用处理日期/时间/日期时间/时间戳的代码,这些语句一致地处理从客户端到服务器的时区转换,还是使用驱动程序中的这些数据类型的遗留代码来实现向后兼容?将此属性设置为'false‘会使"useTimezone“、"useJDBCCompliantTimezoneShift”、"useGmtMillisForDatetimes“和”useFastDateParsing“的效果无效。默认值: true
serverTimezone:覆盖时区检测/映射。当服务器中的时区未映射到Java时区时使用
如果mysql-connector-java是5.1,您应该指定三个属性,如下所示: jdbc:mysql://host:port/dbname?useTimezone=true&useLegacyDatetimeCode=true&serverTimezone=GMT%2B08:00
如果mysql-connector-java为8.0,您应该指定一个属性,如下所示: jdbc:mysql://host:port/dbname?serverTimezone=GMT%2B08:00
发布于 2021-06-17 17:05:17
在我的案例中,最不可能的嫌疑人是jackson。
如果使用Spring Boot,请将以下属性添加到application.properties
spring.jackson.time-zone=Asia/Colombo
我已经在这里详细回答了这个问题,-> https://stackoverflow.com/a/68016006/9183199
https://stackoverflow.com/questions/50066795
复制相似问题