java后端统一处理接收日期格式转换

后端统一处理接收日期格式转换

问题现象

前端传回参数日期格式为yyyy-MM-dd HH:mm:ss,后端默认解析格式是yyyy/MM/dd HH:mm:ss,请求时回报错参数格式错误。

解决方法1:
接收参数的实体类中属性字段上添加@DateTimeFormat(pattern = “yyyy-MM-dd HH:mm:ss”)
列:

			/**
             * 属性addTime( 添加时间)
             */
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
            private Date addTime;

解决方案二:
这样每个接收日期时间的实体类上都要添加麻烦后端数据绑定预处理日期格式

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;
import java.util.Date;


/**
 * 参数预处理
 *
 */
@ControllerAdvice("某某包.某某包.controller")//列@ControllerAdvice("com.nidebao.controller")他会拦截你这个报下的请求
public class DateControllerAdvice {
    /**
     * 将传入的日期格式转换成Date类型
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new DateEditor(true));
    }
}

通过继承PropertyEditorSupport 使用setAsText方法来转换完日期格式并将转换后的值设置回去。

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.util.Date;

/**
 * 处理前端传入日期格式转换
 */
public class DateEditor extends PropertyEditorSupport {

    /**
     * 默认的日期格式
     */
    private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 能够进行转换的日期格式
     */
    public static final String[] DATE_PATTERNS = new String[] { "yyyy", "yyyy-MM", "yyyyMM", "yyyy/MM", "yyyy-MM-dd", "yyyyMMdd", "yyyy/MM/dd", "yyyy-MM-dd HH:mm:ss", "yyyyMMddHHmmss", "yyyy/MM/dd HH:mm:ss" };


    /**
     * 是否将空转换为null
     */
    private boolean emptyAsNull;

    /**
     * 日期格式
     */
    private String dateFormat = DEFAULT_DATE_FORMAT;

    public DateEditor(boolean emptyAsNull) {
        this.emptyAsNull = emptyAsNull;
    }

    public DateEditor(boolean emptyAsNull, String dateFormat) {
        this.emptyAsNull = emptyAsNull;
        this.dateFormat = dateFormat;
    }

    /**
     * 获取日期
     */
    @Override
    public String getAsText() {
        Date value = (Date) getValue();
        return value != null ? DateFormatUtils.format(value, dateFormat) : StringUtils.EMPTY;
    }

    /**
     * 设置日期
     */
    @Override
    public void setAsText(String text) {
        if (text != null) {
            String value = text.trim();
            if (emptyAsNull && StringUtils.isEmpty(text)) {
                setValue(null);
            } else {
                try {
                    setValue(DateUtils.parseDate(value, DATE_PATTERNS));
                } catch (ParseException e) {
                    setValue(null);
                }
            }
        } else {
            setValue(null);
        }
    }


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值