springmvc配置全局日期转换器

https://www.cnblogs.com/kelelipeng/p/13452267.html?ivk_sa=1024320u

 

 1.实体类中加日期格式化注解

 @DateTimeFormat(pattern = "yyyy-MM-dd")

 

2. springmvc配置全局日期转换器

package com.lai.boss.common.utils;
 
public class CustomDateEdtor implements WebBindingInitializer{
    public void initBinder(WebDataBinder binder, WebRequest request) {
        DateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat dateFormat =  new SimpleDateFormat("yyyy-MM-dd");
        try{
            binder.registerCustomEditor(Date.class, new CustomDateEditor(dateTimeFormat, true));
        }catch (Exception e){
            try {
                binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
            } catch (Exception e2) {
                reuturn null;
            }
        }        
    }
}

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  <property name="webBindingInitializer">
    <bean class="com.lai.boss.common.utils.CustomDateEdtor"/>
  </property>
</bean>






3.使用@InitBinder格式化Date类型参数问题
在实际开发中后端某个参数是Date类型,而前端传回来的值是String类型,这时后端接参时不会自动转化为Date类型。需要手动配置,自定义数据的绑定,这个时候就用到了@InitBinder。我们只需要在Controller中配置如下代码就会实现Date类型自动转换。
    
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.springframework.beans.propertyeditors.PropertiesEditor;
 
public class DateFormatEditor extends PropertiesEditor {
    // 日期是否允许为空
    private boolean isAllowEmpty;
    // 日期格式
    private String pattern;
 
    public DateFormatEditor() {
        super();
        this.isAllowEmpty = true;
        this.pattern = "yyyy-MM-dd";
    }
 
    public DateFormatEditor(String pattern, boolean isAllowEmpty) {
        super();
        this.isAllowEmpty = isAllowEmpty;
        this.pattern = pattern;
    }
 
    public boolean isAllowEmpty() {
        return isAllowEmpty;
    }
 
    public void setAllowEmpty(boolean isAllowEmpty) {
        this.isAllowEmpty = isAllowEmpty;
    }
 
    public String getPattern() {
        return pattern;
    }
 
    public void setPattern(String pattern) {
        this.pattern = pattern;
    }
 
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        try {
            if (text != null && !text.equals("")) {
                SimpleDateFormat format = new SimpleDateFormat(pattern);
                setValue(format.parse(text));
            } else {
                if (isAllowEmpty) {
                    setValue(null);
                }
            }
        } catch (ParseException e) {
            System.out.println("格式转换失败!");
            setValue(null);
        }
    }
}



 


@InitBinder
public void initBinder(ServletRequestDataBinder binder) { /** * 自动转换日期类型 */ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); binder.registerCustomEditor(Date.class, new CustomDateEditor(format, true)); }

 

配置完之后,如果前端传来的数据是正常的日期数据是没有问题的,但是如果某个Date类型的字段不是必传的这时也许就会出现前端传回的字段值为null,此时对日期的格式化就会报错。

解决这个问题,其实前后端都可以解决。

这时对后端来说有一个很简单的解决方式,那就是让前端判断当这个日期的字段是空时,就不传这个字段,如果不传这个字段后端就会把这个日期字段置为null,从而不影响程序。

但是如果前端同事不服他们不改的话,那就后端改呗。

后端改其实也挺简单,那就是自定义一个编辑器类,可以写一个内部类继承原有的PropertyEditorSupport这种自定义的其实也很简单,而且还可以支持多种日期格式的转换。
 

 

public class TestController {
 @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        /*** 自动转换日期类型的字段格式
         */
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class,new DateEditor());
    }
 
    private class DateEditor extends PropertyEditorSupport {
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            //转换yyyy-MM-dd HH:mm:ss格式数据
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = null;
            //如果不为空就格式化
             if(!StringUtils.isEmpty(text)){
                try {
                        date = format.parse(text);
                } catch (ParseException e) {
                    //转换为yyyy-MM-dd格式数据
                    format = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        date = format.parse(text);
                    } catch (ParseException e1) {
                    }
                }
            }
            setValue(date);
        }
    }
}

 

 

posted @ 2022-03-02 11:03  kelelipeng  阅读(456)  评论(0编辑  收藏  举报