我注意到当我将某些日期插入我的表时,我的 MySql 数据库从我的 DateTime 对象中减去一个小时。例子:
Insert: 2021-03-29 11:44:14.938
Result: 2021-03-29 10:44:14.938
我正在timestamp
使用 JdbcTemplate.update插入 Java.Sql.Timestamp 对象(如下):
jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
PreparedStatement stmt = con.prepareStatement(
"INSERT INTO Table (date) VALUES (?)");
stmt.setTimestamp(5,timestamp));
return stmt;
}
});
这只发生在 2021 年 3 月 28 日/之后的 DateTime(这是英国的夏令时)。如果我在 3 月 28 日之前插入,不会浪费时间。例子:
Insert: 2021-03-26 11:44:14.938
Result: 2021-03-26 11:44:14.938
我曾尝试使用 Timestamp 而不是 DateTime 作为 MySQL 类型,但没有效果。
有谁知道如何阻止这种行为?
不知道为什么会发生这种情况,但我通过放弃 Java.Sql.Timestamp 转而使用 Java.Time.LocalDateTime 解决了这个问题。
我的插入代码现在如下所示(其中 localDateTime 是 LocalDateTime 而不是 Timestamp 类型):
jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
PreparedStatement stmt = con.prepareStatement(
"INSERT INTO Table (date) VALUES (?)");
stmt.setObject(5,localDateTime));
return stmt;
}
});
MySql 数据库不再自动调整时区。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句