Spring中日期格式出错解决办法

1.用注解的方式

@DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")  用在VO上接收参数    前台传给后台

@JsonFormat(pattern = "HH:mm:ss", timezone = "GMT+8")   用于输出格式化的日期       后台传给前台

2.用配置和方法解决

前台传给后台

在controller需要日期转换的类中加入下面这个方法:

@InitBinder
protected void initBinder(ServletRequestDataBinder binder) {
   DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
   CustomDateEditor dateEditor = new CustomDateEditor(fmt, true);
   binder.registerCustomEditor(Date.class, dateEditor);
}

 

台传给前台

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8


参考:
   注意 @DateTimeFormat 和@JsonFormat 使用场景的不同

       SpringMVC或SpringBoot请求时间参数报错处理方法

 



posted @ 2018-11-06 21:08  damon11  阅读(488)  评论(0)    收藏  举报