前言:除了标题的问题,Redis在存储LocalDateTime的时候也会有相关的问题,解决方案其实也很简单,在相对应的实体类,对LocalDateTime类型加几条注解即可
解决方案
通过LocalDateTime的源码可以发现,问题出现的原因:
1 2 3 4
| @Override public String toString() { return date.toString() + 'T' + time.toString(); }
|
这个问题可以前端处理,也可以后端处理,前端处理就是可以通过格式化的方式去掉T
而后端处理的方式,最简单就是加注解
1 2 3 4 5 6
|
@DateTimeFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8") private LocalDateTime time;
|
- 如果这个时候,后台返回还有T的话,可能是引用了fastJson的api,需要加多一条注解,@JSONField
- 同样适用于Redis保存
- 所以最好用下面的这个
1 2 3 4 5 6 7 8 9
| import com.fasterxml.jackson.annotation.JsonFormat; import com.alibaba.fastjson.annotation.JSONField; import org.springframework.format.annotation.DateTimeFormat; import java.time.LocalDateTime;
@DateTimeFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8") @JSONField(format = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createTime;
|