博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
@DateTimeFormat 和 @JsonFormat 注解
阅读量:7164 次
发布时间:2019-06-29

本文共 1414 字,大约阅读时间需要 4 分钟。

1、DateTimeFormat:入参格式化

  用法:用在类型为Date的字段上,用来接收String类型的字符串,将其转成Date类型

  import org.springframework.format.annotation.DateTimeFormat;

   @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

  pattern值表示接收的字符串类型

2、 @JSONField:出参格式化

·  用法:用在从数据库获取时间传到前端的时候,在数据库显示的是正确的格式,但是获取出来就变成了时间戳,@JSONField注解可以将后台到前台的时间格式保持一致

  导入相关jar包:

com.fasterxml.jackson.core
jackson-annotations
2.8.8
com.fasterxml.jackson.core
jackson-databind
2.8.8
org.codehaus.jackson
jackson-mapper-asl
1.9.13

//设置时区为上海时区,时间格式自己据需求定

@JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")

private Date bindTime;

pattern:我们需要转换成的日期格式

 

3、@JSONField :转成JSONString的时候格式化日期

引入fastsjon jar包:

com.alibaba
fastjson
1.2.28

import com.alibaba.fastjson.annotation.JSONField;

@JSONField(format = "yyyy-MM-dd HH:mm:ss")

private Date modifyTime;

format:我们要格式化后的格式

return JSON.toJSONString(resultMap, SerializerFeature.WriteMapNullValue);

转载于:https://www.cnblogs.com/yangyongjie/p/10953906.html

你可能感兴趣的文章