我想将时间戳保存到数据库,而不会被jdbc驱动程序转换为本地时区。目前只有MySQL和PostgreSQL对我很重要,但如果有独立于数据库的解决方案,我将不胜感激。JDBC/MySQL:始终使用UTC保存时间戳
实施例:
// i want that to be saved as 1970-01-01 00:00:00
TimeStamp ts = new java.sql.Timestamp(0);
// gets transformed to local time by jdbc driver (in my case to 1970-01-01 01:00:00)
st.setTimestamp(0, new java.sql.Timestamp(0));
// only works using postgres (mysql connector seems to ignore the calendar)
// postgres => 1970-01-01 00:00:00
// mysql => 1970-01-01 01:00:0
Calendar calutc = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
st.setTimestamp(0, new java.sql.Timestamp(0), utccal);
我已经尝试来计算得到转化为正确的值的时间戳值(例如-3600000 => 1970-01-01 00:00:00在我的时区)但这对于日间节省时间变化附近的日期postgres无效。
我使用'&serverTimezone = UTC&useLegacyDatetimeCode = false'成功,没有serverTimezone jdbc驱动程序抱怨错误的时区。 – snowindy 2014-11-04 16:51:26
完整的URL,供参考:jdbc:mysql:// localhost:3306/pm_func?characterEncoding = UTF-8&useEncoding = true&autoReconnect = true&serverTimezone = UTC&useLegacyDatetimeCode = false – snowindy 2014-11-04 16:51:45