Article directory
-
- @JsonFormat and @DateTimeFormat use
-
- `0 possible error screenshots`
- `1 Add custom Json return annotation @JsonSerialize` to the attribute
- `2 Create a new entity class CustomDateTimeSerializer`
- `3 Front-end transmission and back-end format conversion (ISO date format) to (Data)`
- `4 An annotation to solve the double-ended conversion problem`
@JsonFormat and @DateTimeFormat use
Refer to this article to use @JsonFormat and @DateTimeFormat
0 可能错误截图
1 在属性上加自定义Json返回注释 @JsonSerialize
@TableField(value = "create_time")
@JsonSerialize(using = CustomDateTimeSerializer.class)
private Date createTime;
2 新建实体类 CustomDateTimeSerializer
public class CustomDateTimeSerializer extends JsonSerializer<Date>{
@Override
public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers)
throws IOException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
gen.writeString(sdf.format(value));
}
}
3 前端传后端格式转换(ISO 日期格式)转(Data)
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date updateTime;
4 一个注释解决双端转化问题
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date updateTime;