让你下班早走十分钟的注解
Spring时间解析注解——来源Spring框架自带
@DateTimeFormat
org.springframework.format.annotation.DateTimeFormat
功能:字符串格式化成日期,常用于前台向后端传参,解析json串
支持常见标准时间格式:①yyyy-MM-dd HH:mm:ss②HH:mm:ss④……
@DateTimeFormat(style="HH:mm:ss")
@ApiModelProperty(value = "工作起始时间")
private Time workingTimeStar;
@DateTimeFormat属性讲解
属性style: 允许我们使用两个字符的字符串来表明怎样格式化日期和时间。第一个字符表明了 日期的格式,第二个字符表明了时间的格式。
Pattern: 属性允许我们使用自定义的日期/时间格式。该属性的值遵循java标准的date/time格式规范
PS:测试发现style Pattern都可以设置为yyyy-MM-dd HH:mm:ss或HH:mm:ss 可成功解析传入的json串(TODO未进一步研究)
Jackson将时间格式化时间注解——来源Jackson插件
@JsonFormat
com.fasterxml.jackson.annotation.JsonFormat
功能:把时间格式化为字符串,常用数据展示,数据转VO,数据转json
支持常见标准时间格式:①yyyy-MM-dd HH:mm:ss②yyyy-MM-dd③HH:mm:ss④……
@JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8")
@ApiModelProperty(value = "创建时间")
private Date createTime;
timezone 时区 东八区(北京时间)
@JSONField
com.alibaba.fastjson.annotation.JSONField
用法:
name:@JSONField(name=”json中的name”)主要用于指定前端传到后台时对应的key值,默认key=private String name,name
format :@JSONField(format=”yyyy-MM-dd”)主要用于解析日期
@JSONField(format="yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "创建时间")
private Date createTime;
名称 | 功能 |
NumberFormatter | 实现Number与String之间的解析与格式化 |
CurrencyFormatter | 实现Number与String之间的解析与格式化(带货币符号) |
PercentFormatter | 实现Number与String之间的解析与格式化(带百分数符号) |
DateFormatter | 实现Date与String之间的解析与格式化 |
NumberFormatAnnotationFormatterFactory | @NumberFormat注解,实现Number与String之间的解析与格式化,可以通过指定style来指示要转换的格式(Style.Number/Style.Currency/Style.Percent),当然也可以指定pattern(如pattern=“#.##”(保留2位小数) ),这样pattern指定的格式会覆盖掉Style指定的格式 |
JodaDateTimeFormatAnnotationFormatterFactory | @DateTimeFormat注解,实现日期类型与String之间的解析与格式化这里的日期类型包括Date、Calendar、Long以及Joda的日期类型。必须在项目中添加Joda-Time包 |
拓展
@InitBinder对表单数据绑定解决String转换Date问题
单独写类型转换器
spring boot结合jackson全局配置(自动转换时间)https://www.cnblogs.com/liaojie970/p/9396334.html
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
JAVA--fastJSON+自定义注解格式化实体字段 https://blog.csdn.net/yu459348471/article/details/75414749