升级mysql-connector-java至8.0.27导致时间格式异常

1.前因

  公司要求升级mysql-connectot-java.jar至最新版本8.0.27,然后在查询数据是时间格式时,时间格式异常。具体表现为“2021-12-30T00:09:12、2021-12-30T07:00”这种格式。

如果仅仅是多一个T可以替换来解决,但是发现存在秒为00的时候,秒会丢失。这样就需要从源头上考虑了

2.解决过程和解决方法

在网上尝试了很多方法都未解决问题。后来观察发现在解析返回数据时原来是用SqlRowSet.getString,尝试用SqlRowSet.getTime/getDate。发现会报错java.time.LocalDateTime cannot cast to xxx。

于是改写为以下代码

        LocalDateTime localDateTime = LocalDateTime.parse(rs.getString(cloumn));
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
        Date date = Date.from(zonedDateTime.toInstant());
        String formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);

至此问题解决。如果以上内容有何纰漏,欢迎大家在评论区指出。有何疑问也可以在评论区交流!

谢谢观看哈。

参考链接:https://blog.csdn.net/hspingcc/article/details/73332380

 

posted @ 2021-12-30 16:50  hard-bokeyuan  阅读(1601)  评论(0)    收藏  举报