How to avoid the url Time Zone traps in mysql
Preface
Recently, when using mysql's jar above 6.0.x, You need to specify serverTimezone in the url of the Code. An exception occurs:
1. serverTimezone not specified
Configure url in xml
<property name="url" value="jdbc:mysql://localhost:3306/mybatisstudy"/>
Exception
Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���ʱ��' 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.
You must configure the server or JDBC Driver (through the serverTimezone Configuration Attribute). If you want to use time zone support, you need to use a more detailed time zone value.
2. Online Solutions
Add a parameter after the url? ServerTimezone = utc
<property name="url" value="jdbc:mysql://localhost:3306/springdatastudy?serverTimezone=UTC"/>
. Problems Encountered
Although there is no error in the time zone program added above, we encountered a problem when inserting data into the database using java code.
For example, the insert time in the java code is 17:29:56.
However, the time displayed in the database is: 09:29:56
3. Root Cause
Because of time zone settings.
UTC indicates the global standard time, but we use the Beijing Time Zone (UTC + 8), which is eight hours ahead of UTC.
UTC + (+ 0800) = Local (Beijing) Time
4. Solution
The Time Zone of the url uses the standard Chinese time. The same is true.serverTimezone=Asia/Shanghai
4.1 Use java code to obtain the local time zone id
Calendar cal = Calendar.getInstance();TimeZone timeZone = cal.getTimeZone();System.out.println(timeZone.getID());System.out.println(timeZone.getDisplayName());
Asia/Shanghai China Standard Time
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.