简体   繁体   中英

Setting a time zone on a JDBC connection

I'm running some java on a Linux box whose time zone is:

% timedatectl
...
America/Los_Angeles (PDT, -700)

During MySQLDatabase.getConnection() I get error:

java.sql.SQLException: The server time zone value 'PDT' 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 specific time zone value if you want to utilize time zone support.

I cannot change the server time zone, so I'm looking to tell JDBC about it. Consulting the documentation here it gives me three options, and I am lost attempting to apply them.

Option 1: I'm looking for a setServerTimezone method on the MysqlDataSource and find none.

Option 2: I don't seem to be calling either of those methods.

Option 3: I don't have a connection URL either, because I start with MysqlDataSource , set username etc and get a Connection from there.

How do I do this?

I have not figured out the answer to the original question, but I have figured out another way of solving it:

  • The default time zone on the "server" (Mariadb/MySQL) refers to the database daemon, not the server box. (It's ambiguous in the docs)
  • You can set a default time zone for the database daemon. If none is set (which was my case), it is taken from the server operating system.
  • To set the daemon default time zone to UTC, edit /etc/mysql/my.conf (or equivalent) and add default_time_zone = +0:00 to the [mysqld] section.
  • You need to use an offset, not a named time zone, unless you have the timezone-related tables in the mysql database populated (they always exist, but are empty by default).

PS There are people at ex-Sun/Oracle who understand API design. What about you dudes, if you read this, run some internal classes to teach those who do not understand yet? And, dear docs people, if you write that X is a property of Y in your docs, can you make sure Y.getX() exists, please?

On my Rasperry Pi I just had to change the time zone in Rasperian.

Hope that will work, Philipp

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