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中是传给被调用接口之前传出的格式控制

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值