springboot 日期类型处理

1. 日期类型输出参数处理

默认日期格式只支持:2017-07-01T10:10:01

修改为2017-07-01 10:10:01

只需要修改配置文件即可:

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

 

1. 日期类型输入参数处理

默认日期格式只支持:2017/07/01

修改为支持2017-07-01日期格式

@ControllerAdvice
public class GlobalHandler {

    @InitBinder
    public void initBinder(WebDataBinder binder) {

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

    }
}

看到网友介绍的可以使用registerCustomEditor的重载方法,使用日期类型支持多种格式,但好像是需要在具体的日期字段上添加注解才可行,我没有尝试。

还有网友介绍的:通过继承WebMvcConfigurationSupport

@Configuration 
public class WebMvcConfig extends WebMvcConfigurationSupport {

    /**
     * 添加自定义的Converters和Formatters.
     */
    @Override
    protected void addFormatters(FormatterRegistry registry) {
       registry.addConverter(new StringToDateConverter());
    }
}

这种方式会使用application.yml对日期的配置失效。

为了解决配置失效的问题,尝试实现WebMvcConfigurer,但又报swagger的错误。

所以最后选择了上述的方案。

转载于:https://www.cnblogs.com/hankuikui/p/10172691.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值