54

MySQL runs with timezone "GMT+8", but Tomcat with "GMT". When I save datetime to my database, everything seems to be OK, but when I check the datetime value in the database, I see the "GMT" value.

Also when I try get the value from the database the value is changed, seems like the value in the database is taken as "GMT+8", so Java changes the value to "GMT".

I have set the connection URL like this:

useTimezone=true&serverTimezone=GMT

but it does not work.

3
  • Do you only get the "wrong" timezone, or is the value itself wrong? Commented Sep 30, 2011 at 4:33
  • I change the config to useGmtMillisForDatetimes=true&serverTimezone=GMT. seems it's the behavior I want. but still confuse. Commented Sep 30, 2011 at 5:54
  • See stackoverflow.com/a/64031088/2170831. Commented Sep 23, 2020 at 16:02

6 Answers 6

92

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

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

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).

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

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).

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

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.


Note:

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.

Sign up to request clarification or add additional context in comments.

11 Comments

Thank you for the useLegacyDatetimeCode suggestion! Note: This also solved my problems working with Joda Time (via the usertypes hibernate add-on) as well.
So with serverTimezone and useLegacyDatetimeCode added to my connection string, I can send a Timestmp and it gets recorded correctly in a DateTime column as it's UTC time. I can see it on my MySQL console as such. When I do a SELECT via JDBC and try to get the data out, it still automatically converts it in the JDBC Layer. I've tried getObject() and geTimestmp (both without and with a UTC calendar object) and it keeps coming back in local time.
Turns out this was the setting I needed to solve a production problem. Locally we could not reproduce it. But trying this in the driver on production eliminated the stall we were seeing after 10-30 seconds of processing.
@djsumdog - Timezone info is not stored in the Timestamp class. So when you print a date object to the console it will be formatted in the JVM time zone.
try String url = "jdbc:mysql://localhost?tz=useLegacyDatetimeCode=false&serverTimezone=America/New_York";. For my application this sample link works: url: jdbc:mysql://localhost:3306/trip_planner?tz=useLegacyDatetimeCode=false&serverTimezone=Europe/Berlin
|
14

JDBC uses a so-called "connection URL", so you can escape "+" by "%2B", that is

useTimezone=true&serverTimezone=GMT%2B8

1 Comment

Currently mysql-connector-java 5.1.33-37 complaint is caused always by a bug, because in most cases serverTimeZone parameter shouldn't be necessary. Refer this post: stackoverflow.com/questions/26515700/…
1

For applications such as Squirrel SQL Client (http://squirrel-sql.sourceforge.net/) version 4 you can set "serverTimezone" under "driver properties" to GMT+1 (example of timezone "Europe/Vienna).

2 Comments

Thank you, that worked! I also wanted to note that IST didn't change anything while GMT+1 did the trick.
Thank YOU! I never estimated my answer would be of any relevance ... :-)
0

Is there a way we can get the list of supported timeZone from MySQL ? ex - serverTimezone=America/New_York. That can solve many such issue. I believe every time you need to specify the correct time zone from the Application irrespective of the DB TimeZone.

1 Comment

You can use TimeZone.getAvailableIDs() method. basically returns a String array. Which you can printout and check the list of available timezones.
0

Works for me

spring.datasource.url= jdbc:mysql://localhost:3306/catalog?serverTimezone=UTC

Comments

0

Change databasename, username and userpassword

 connection = DriverManager.getConnection("jdbc:mysql://localhost/databasename?user=username&password=userpassword"
                                    + "&userUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetime=false&serverTimezone=UTC");
        

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.