springboot时间注解
/**
* 创建时间
springboot中文*/
@JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern ="yyyy-MM-dd HH:mm:ss")
private LocalDateTime startTime;
@JsonFormat注解是⼀个时间格式化注解,⽐如我们存储在mysql中的数据是date类型的,当我们读取出来封装在实体类中的时候,就会变成英⽂时间格式,⽽不是yyyy-MM-dd HH:mm:ss这样的中⽂时间,因此我们需要⽤到JsonFormat注解来格式化我们的时间
⼩编理解是把数据库的时间转化为⼈性化的json串,要不然可能就是这样的2021-08-09T00:00:00.000+0000,有点反⼈性了
@DateTimeFormat 把前端传过来的时间转化为数据库的时间
前后的时间⽐数据库存储的时间多了⼋个⼩时,出现了这个时间不匹配。我⽬前的环境是:、
springboot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
mysql版本:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
我以前的数据库连接: jdbc:mysql://127.0.0.1:3306/online_coupon?useUnicode=true&characterEncoding=UTF-
8&useSSL=false&serverTimezone=GMT%2B8
在⽹上查看资料发现,是数据库版本提⾼之后出现的问题: 把数据库连接更改为serverTimezone=Asia/Shanghai即可。完整链接:jdbc:mysql://127.0.0.1:3306/online_coupon?useUnicode=true&characterEncoding=UTF-
8&useSSL=false&serverTimezone=Asia/Shanghai