Datetime behind an hour after insertion. Daylight savings

后端 未结 2 1125
迷失自我
迷失自我 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:05

    You can use OffsetDateTime. Since JDBC 4.2 , you can use java.time types directly with JDBC:

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
    
    OffsetDateTime odt = LocalDateTime.parse("2021-03-29 11:44:14.938", dtf)
                                        .atZone(ZoneId.of("Europe/London"))
                                        .toOffsetDateTime();
    
    PreparedStatement st = conn.prepareStatement("INSERT INTO mytable (columnfoo) VALUES (?)");
    st.setObject(1, odt);
    st.executeUpdate();
    st.close();
    

    Learn about the modern date-time API from Trail: Date Time.

提交回复
热议问题