如何在java连接中更改mysql时区 [英] How to change mysql timezone in java connection

查看:1279
本文介绍了如何在java连接中更改mysql时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mysql在GMT + 8运行,但tomcat在GMT运行。
当将日期时间保存到数据库时,似乎运行正常,当我检查DB中的日期时间值时,我看到GMT值。
但是当我尝试从DB获取值时,值被更改,似乎DB中的值被视为GMT + 8,因此java将值更改为GMT。

Mysql run at GMT+8 but tomcat run at GMT. when save datetime to database, seem run ok, when i check the datetime value in DB, i see the GMT value. But when I try get the value from DB, the value is changed, seem the value in DB is taken as GMT+8, so java change the value to GMT.

我尝试设置连接url add

I try set the connection url add

useTimezone=true&serverTimezone=GMT

但不起作用

推荐答案

useTimezone是一种较旧的解决方法。 MySQL团队最近重写了setTimestamp / getTimestamp代码,但只有在设置连接参数useLegacyDatetimeCode = false并且使用最新版本的mysql JDBC连接器时才会启用它。例如:

useTimezone is an older workaround. MySQL team rewrote the setTimestamp/getTimestamp code fairly recently, but it will only be enabled if you set the connection parameter useLegacyDatetimeCode=false and you're using the latest version of mysql JDBC connector. So for example:

String url =
 "jdbc:mysql://localhost/mydb?useLegacyDatetimeCode=false

如果你下载mysql-connector源代码并查看setTimestamp,很容易看出发生了什么:

If you download the mysql-connector source code and look at setTimestamp, it's very easy to see what's happening:

如果使用旧日期时间代码= false,则调用newSetTimestampInternal(...)。然后,如果传递给newSetTimestampInternal的Calendar为NULL,则日期对象的格式为数据库的时区:

If use legacy date time code = false, newSetTimestampInternal(...) is called. Then, if the Calendar passed to newSetTimestampInternal is NULL, your date object is formatted in the database's time zone:

this.tsdf = new SimpleDateFormat("''yyyy-MM-dd HH:mm:ss", Locale.US);
this.tsdf.setTimeZone(this.connection.getServerTimezoneTZ());
timestampString = this.tsdf.format(x);

日历为空是非常重要的 - 所以请确保你正在使用:

It's very important that Calendar is null - so make sure you're using:

setTimestamp(int,Timestamp).

...不是setTimestamp (int,Timestamp,Calendar)。

... NOT setTimestamp(int,Timestamp,Calendar).

现在应该是显而易见的如果您使用java.util.Calendar和调用setTimestamp(1,myDate)构建日期:2011年1月5日凌晨3:00在America / Los_Angeles(或您想要的任何时区),那么它将占用您的日期,使用SimpleDateFormat在数据库时区中对其进行格式化。因此,如果您的数据库位于America / New_York,它将构建要插入的字符串'2011-01-05 6:00:00'(因为纽约比洛杉矶提前3小时)。

It should be obvious now how this works. If you construct a date: January 5, 2011 3:00 AM in America/Los_Angeles (or whatever time zone you want) using java.util.Calendar and call setTimestamp(1, myDate), then it will take your date, use SimpleDateFormat to format it in the database time zone. So if your DB is in America/New_York, it will construct the String '2011-01-05 6:00:00' to be inserted (since NY is ahead of LA by 3 hours).

要检索日期,请使用getTimestamp(int)(不使用Calendar)。它将再次使用数据库时区来构建日期。

To retrieve the date, use getTimestamp(int) (without the Calendar). Once again it will use the database time zone to build a date.

注意:现在,网络服务器时区完全不相关!如果你不喜欢将useLegacyDatetimecode设置为false,网络服务器时区用于格式化 - 增加了许多混淆。

Note: The webserver time zone is completely irrelevant now! If you don't set useLegacyDatetimecode to false, the webserver time zone is used for formatting - adding lots of confusion.

注意:

MySQL我可能抱怨服务器时区不明确。例如,如果您的数据库设置为使用EST,则Java中可能存在多个可能的EST时区,因此您可以通过告诉它确切的数据库时区来澄清这一点:

It's possible MySQL my complain that the server time zone is ambiguous. For example, if your database is set to use EST, there might be several possible EST time zones in Java, so you can clarify this for mysql-connector by telling it exactly what the database time zone is:

String url =
 "jdbc:mysql://localhost/mydb?useLegacyDatetimeCode=false&serverTimezone=America/New_York";

如果它抱怨,你只需要这样做。

You only need to do this if it complains.

这篇关于如何在java连接中更改mysql时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆