spring boot 统一处理时间参数

在网上面搜了很多都发现有点没有描述清楚应该分两种情况

1.对于非请求体json参数(url参数,表单参数)

  我选择的是添加一个时间转换器

      

package com.htsc.thfx.framework.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName: InterceptorConfig
 * @Description: TODO
 * @author: K0180041
 * @date: 2018年7月25日 下午6:51:48
 */
@Configuration
public class WebMvcConfigurerConfig extends WebMvcConfigurerAdapter {

 

    @Override
    public void addFormatters(FormatterRegistry registry) {
        // lambda 会报错
        registry.addConverter(new Converter<String, Date>() {
            @Override
            public Date convert(String stringDate) {
                SimpleDateFormat simpleDateFormat;
                if (stringDate.contains(" ")) {
                    simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                } else {
                    simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                }
                try {
                    return simpleDateFormat.parse(stringDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return null;
            }


        });
    }
}

 

2.对于请求体json参数

在时间类型添加暂时没有找到合适的全局处理的方式,思路应该是修改json的序列化的方式......

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")

转载于:https://www.cnblogs.com/ronniery/p/11125238.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值