zoukankan      html  css  js  c++  java
  • 前后端时间转化

    方法一、

    在Controller里面写入相应的方法 加入

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        //binder.setFieldDefaultPrefix(""); //设置默认前缀
        //binder.setDisallowedFields();
       
    SimpleDateFormat d1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat d2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        binder.registerCustomEditor(Date.class, new CustomDateEditor(d1, true));
        binder.registerCustomEditor(Date.class, "time1", new CustomDateEditor(d2, true));
    }

     

    方法一,全局注入

    package com.jd.irm.config.paramsFormat;

    import org.springframework.beans.propertyeditors.CustomDateEditor;
    import org.springframework.web.bind.WebDataBinder;
    import org.springframework.web.bind.annotation.InitBinder;

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

    /**
     * @Author: husToy.Wang
     * @Date: 2019/8/22 10:14
     * @Version 1.0
     */
    //@ControllerAdvice
    public class AppControllerAdvice {
        @InitBinder
        public void initBinder(WebDataBinder binder){
            /*以下方法二选一,第一个无需添加MulitFormatDateEditor这个类*/

            //方法1:支持一个日期格式
           
    binder.registerCustomEditor(Date.class,
                    new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),true));

            ////方法2:支持两个日期格式
            ////true 允许属性为空verficationController
           
    binder.registerCustomEditor(Date.class,
                    new MulitFormatDateEditor(new SimpleDateFormat("yyyy-MM-dd"),
                            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),true));

        }

    }
    
    
    
    package com.jd.irm.config.paramsFormat;


    import org.springframework.util.StringUtils;

    import java.beans.PropertyEditorSupport;
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.util.Date;

    /**
     * @Author: husToy.Wang
     * @Date: 2019/8/22 10:16
     * @Version 1.0
     */
    public class MulitFormatDateEditor extends PropertyEditorSupport {

        //日期格式1
       
    private final DateFormat dateFormat;
        //日期格式2
       
    private final DateFormat dateFormat2;

        //是否允许为空
       
    private final boolean allowEmpty;
        //日期长度
       
    private final int exactDateLength;


        public MulitFormatDateEditor(DateFormat dateFormat, DateFormat dateFormat2, boolean allowEmpty) {
            this.dateFormat = dateFormat;
            this.dateFormat2 = dateFormat2;
            this.allowEmpty = allowEmpty;
            this.exactDateLength = -1;
        }

        public MulitFormatDateEditor(DateFormat dateFormat, DateFormat dateFormat2, boolean allowEmpty, int exactDateLength) {
            this.dateFormat = dateFormat;
            this.dateFormat2 = dateFormat2;
            this.allowEmpty = allowEmpty;
            this.exactDateLength = exactDateLength;
        }


        /*
         *
    使用指定的日期格式,解析给定文本的日期。
         * */
       
    @Override
        public void setAsText(String text) throws IllegalArgumentException {
            if(this.allowEmpty && StringUtils.hasText(text)){
                setValue(null);
            }
            else if(text != null && this.exactDateLength >= 0 && text.length() != this.exactDateLength){
                throw new IllegalArgumentException("不能解析这个日期,长度为:"+this.exactDateLength);
            }
            else{
                try {
                    if(text != null){
                        setValue(this.dateFormat.parse(text));
                    }
                }catch (ParseException ex){
                    try {
                        if(text != null){
                            setValue(this.dateFormat2.parse(text));
                        }
                    }catch (ParseException ex2){
                        throw new IllegalArgumentException("不能解析这个日期"+ex.getMessage(),ex);
                    }
                }
            }
        }

        /**
         *
    将日期格式化为字符串,使用指定的日期格式。
         */
       
    @Override
        public String getAsText() {
            Date value = (Date)getValue();
            return (value != null ? this.dateFormat2.format(value):"");
        }



    }

     

    建议

    直接在Controller写好,并指定字段,这样更加的清晰,哪个实体里,哪个属性要指定

  • 相关阅读:
    case when then else end
    spark读文件写入mysql(scala版本)
    mysql语句
    spark读文件写mysql(java版)
    spark的广播变量
    hive,把一个表中计算好的数据,存到另一个外部表中
    spark操作hive方式(scala)
    spark sql启动优化
    hive on spark (spark2.0.0 hive2.3.3)
    hive优化,开启压缩功能
  • 原文地址:https://www.cnblogs.com/leigepython/p/11393400.html
Copyright © 2011-2022 走看看