简体   繁体   中英

JDBC SQL Time zone

I'm using Spring with 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:

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:

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.

My question is: How can I tell MySQL or Spring JDBC not to use the client time zone, and simply to always use 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)

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.

It sounds like the JDBC driver isn't properly detecting Mysql's time zone setting. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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