Datetime behind an hour after insertion. Daylight savings

后端 未结 2 1124
迷失自我
迷失自我 2021-01-27 02:35

I\'ve noticed that my MySql database is subtracting an hour from my DateTime objects when I insert certain dates to my tables. Example:

Insert: 2021-03-29 11:44:1         


        
2条回答
  •  孤城傲影
    2021-01-27 03:32

    Not sure why this was happening, but I fixed the problem by ditching Java.Sql.Timestamp in favour of Java.Time.LocalDateTime.

    My insertion code now looks like below (where localDateTime is of type LocalDateTime rather than 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;
        }
    });
    

    The MySql database no longer automatically adjusts for timezone.

提交回复
热议问题