Date日期类型传入传出问题

1、在javaweb项目中,我们会接受前台传入的date类型

      在接受的过程中,经常会遇到日期转换异常,前台传的格式可能是2019-04-03 00:00:00或者2019-09-19T16:00:00.000Z,后台后者如果用Date对象来接受可能会出现问题,在你的controller中添加如下配置可以解决此问题,

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

      此处"2019-09-19T16:00:00.000Z"此类格式可能还会有问题,可以尝试前端修改传参格式

 

2、我们也可以控制调用别人接口时传出的格式,

      在application.properties文件中添加如下配置

      spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
      spring.jackson.time-zone=GMT+8
      spring.jackson.parser.allow-unquoted-control-chars=true
      spring.jackson.parser.allow-single-quotes=true
      spring.jackson.parser.allow-backslash-escaping-any-character=true

 

注: 1中是接受时候接收方对于时间格式的转换,2中是传给被调用接口之前传出的格式控制

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章