Life has its own fate, and meeting may not be accidental.

0%

spring-bootsql导出date字段时区错误以及格式转换

sql添加了个插入自动有个更新时间的字段,但是时区和时间类型老是出错

先设置更新时间的字段类型为timestamp,在springboot导出的时候,需要在application.yml中的文件中把mysql的配置文件后面加入

1
2
3
useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
->
serverTimezone=GMT%2B8

导出的时间类型可能是

1
-> Tue Jan 17 07:00:28 GMT 2020

可以利用SimpleDateFormat类来进行转换为yyyy-MM-dd HH:mm:ss类型

1
2
3
4
5
6
7
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

formatter.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));

formatter.format(log.gettime());

-> 2020-6-6 16:25:30