My Java:
Date parsedDate = dateFormat.parse(timestamp);
Timestamp dbTimestamp = new Timestamp(parsedDate.getTime());
insertTimestampPreparedStatement.setTimestamp(3, dbTimestamp);
PhpMyAdmin/MySQL always displays this in my local time, even when I do
insertTimestampPreparedStatement.setTimestamp(3, dbTimestamp, Calendar.instance(TimeZone.getTimeZone("UTC"));
I'm not sure if MySQL stores them aware of timezone, and the only way I checked this was updating a timezone row to NOW() directly in phpmyadmin, and it then showed me a time in GMT. So either there is a bug in phpmyadmin/mysql or my code is not sending over timestamps in the correct timezone.
How do I get this to work in UTC?
解决方案
A JDBC driver must use the local timezone when retrieving or storing timestamps, unless a Calendar with the specific timezone is provided.
Unfortunately, the JDBC spec isn't always clear on how drivers should behave, and it looks like MySQL does it wrong. For MySQL you can force the timezone it uses by specifying one or more options in the connection string serverTimeZone and related properties. See http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html for a full list.