使⽤@DateTimeFormat注解的时候报错
使⽤Springboot框架搭建项⽬
@DateTimeFormat作⽤是前后到后台的时间格式的转换,使⽤"yyyy-MM-dd"格式的字符串传⼊⽇期类型数据是⼊参转换没有问题,使⽤"yyyy-MM-dd HH:mm:ss"格式时间字符串就会报错
@Data
public class DemoVO {
private Date inputTime;
}
报错信息:
error parse new
"message": "Could not read document: Can not deserialize value of type java.util.Date from String \"2016-03-03 12:12:12\": not a valid representation (error: Failed to parse Date value '2016-03-03 12:12:12': Can not parse date \"2016-03-03
12:12:12Z\": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'', parsing fails (leniency? null))\n at [Source:
java.io.PushbackInputStream@541af790; line: 3, column: 14] (through reference chain:
com.ity.vo.DemoVO[\"inputTime\"]); nested exception is
com.fasterxml.InvalidFormatException: Can not deserialize value of type java.util.Date from String \"2016-03-03 12:12:12\": not a valid representation (error: Failed to parse Date value '2016-03-03 12:12:12': Can not parse date \"2016-03-03 12:12:12Z\": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'', parsing fails (leniency? null))\n at [Source: java.io.PushbackInputStream@541af790; line: 3, column: 14] (through reference chain:
com.ity.vo.DemoVO[\"inputTime\"])",
原因是springboot默认采⽤jackson,⽽jackson只能识别以下⼏种⽇期格式
"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
"yyyy-MM-dd";
"EEE, dd MMM yyyy HH:mm:ss zzz";
long类型的时间戳(毫秒时间戳)
解决办法有以下⼏种:
1.、采⽤long时间戳,如:1537191968000
2、在传参的对象上加上@JsonFormat注解并且指定时区
@JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
如果项⽬中使⽤json解析框架为fastjson框架,在实体字段上使⽤@JsonFormat注解格式化⽇期
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
3、采⽤全局处理⽅式统⼀处理,推荐这个做法,重写springboot默认转换
public class MyDateFormat extends DateFormat {
private DateFormat dateFormat;
private SimpleDateFormat format1 = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
public MyDateFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat;
}
@Override
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {        return dateFormat.format(date, toAppendTo, fieldPosition);
}
@Override
public Date parse(String source, ParsePosition pos) {
Date date = null;
try {
date = format1.parse(source, pos);
} catch (Exception e) {
date = dateFormat.parse(source, pos);
}
return date;
}
// 主要还是装饰这个⽅法
@Override
public Date parse(String source) throws ParseException {
Date date = null;
try {
// 先按我的规则来
date = format1.parse(source);
} catch (Exception e) {
// 不⾏,那就按原先的规则吧
date = dateFormat.parse(source);
}
return date;
}
// 这⾥装饰clone⽅法的原因是因为clone⽅法在jackson中也有⽤到
@Override
public Object clone() {
Object format = dateFormat.clone();
return new MyDateFormat((DateFormat) format);
}
}
@Configuration
public class WebConfig {
@Autowired
private Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder;
@Bean
public MappingJackson2HttpMessageConverter MappingJsonpHttpMessageConverter() {
ObjectMapper mapper = jackson2ObjectMapperBuilder.build();
// ObjectMapper为了保障线程安全性,⾥⾯的配置类都是⼀个不可变的对象
// 所以这⾥的setDateFormat的内部原理其实是创建了⼀个新的配置类
DateFormat dateFormat = DateFormat();
mapper.setDateFormat(new MyDateFormat(dateFormat));
MappingJackson2HttpMessageConverter mappingJsonpHttpMessageConverter = new MappingJackson2HttpMessageConverter(                mapper);
return mappingJsonpHttpMessageConverter;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。