@DateTimeFormat 和 @JsonFormat 注解:Spring Boot 日期时间格式处理
@DateTimeFormat 和 @JsonFormat 是两个注解,用于在 Spring Boot 中处理日期和时间格式的问题。
'@DateTimeFormat' 注解用于指定 Java 对象中的日期/时间格式,例如:
@DateTimeFormat(pattern = 'yyyy-MM-dd')
private Date birthday;
'@JsonFormat' 注解用于指定 JSON 序列化和反序列化时的日期/时间格式,例如:
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = 'yyyy/MM/dd')
private Date birthday;
两者的区别在于,'@DateTimeFormat' 注解只在 Java 对象中起作用,用于指定日期/时间的格式化方式,而 '@Jsonformat' 注解则不仅在 Java 对象中起作用,还在 JSON 序列化和反序列化时起作用,用于指定日期/时间的格式化方式。
总的来说,'@DateTimeFormat' 注解用于 Java 对象,而 '@Jsonformat' 注解用于 Java 对象和 JSON 序列化/反序列化。
原文地址: https://www.cveoy.top/t/topic/ngB1 著作权归作者所有。请勿转载和采集!