简体   繁体   English

JDBC SQL时区

[英]JDBC SQL Time zone

I'm using Spring with apache commons BasicDataSource. 我正在将Spring与Apache Commons BasicDataSource一起使用。

The time zone shows as GMT via: 时区通过以下时间显示为格林尼治标准时间:

SELECT @@global.time_zone, @@session.time_zone;

My input is in epoch time, 1386831420000 and 1386833220000, so the query should be like this: 我的输入是在新纪元时间1386831420000和1386833220000,因此查询应如下所示:

SELECT * FROM table WHERE AND arrival_time BETWEEN '2013-12-12 06:57:00' AND '2013-12-12 07:27:00';

I enabled SQL profiing, and this is the query that actually gets executed, so I don't get the correct results: 我启用了SQL配置,这实际上是要执行的查询,所以我没有得到正确的结果:

SELECT * FROM table WHERE AND arrival_time BETWEEN '2013-12-12 01:57:00' AND '2013-12-12 02:27:00';

Notice that the times are off by 5 hours, since I am EST-5, and the time should be in GMT. 请注意,由于我是EST-5,所以时间缩短了5个小时,时间应该是格林尼治标准时间(GMT)。

My question is: How can I tell MySQL or Spring JDBC not to use the client time zone, and simply to always use GMT? 我的问题是:如何告诉MySQL或Spring JDBC不要使用客户端时区,而要始终使用GMT?

Please comment if there is any detail I could add to solve the issue. 如果有任何我可以解决的细节,请发表评论。

Try explicitly converting the date string into a date type using TO_DATE (or implementation specific date converter function) 尝试使用TO_DATE (或实现特定的日期转换器函数)将日期字符串显式转换为日期类型

SELECT * 
FROM table 
WHERE arrival_time BETWEEN 
TO_DATE('2013-12-12 06:57:00', 'YYYY-MM-DD HH24:MI:SS')
AND 
TO_DATE('2013-12-12 07:27:00', 'YYYY-MM-DD HH24:MI:SS')

The above example is in Oracle SQL but the principle of explicitly casting a date string to a date type is common throughout SQL implementations. 上面的示例在Oracle SQL中,但是在整个SQL实现中,将日期字符串显式转换为日期类型的原理很普遍。

It sounds like the JDBC driver isn't properly detecting Mysql's time zone setting. 听起来JDBC驱动程序没有正确检测Mysql的时区设置。 You may need to specify the server's time zone in the connection string. 您可能需要在连接字符串中指定服务器的时区。 Try adding 尝试添加

serverTimezone=UTC&useTimezone=true

to the connection string. 连接字符串。 For more information, refer to the JDBC doc at http://cs.wellesley.edu/~cs304/jdbc/connector-j.html#connector-j-reference 有关更多信息,请参见http://cs.wellesley.edu/~cs304/jdbc/connector-j.html#connector-j-reference上的JDBC文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM