zoukankan      html  css  js  c++  java
  • 自定义转换器:日期转换器

    A)第一种方式:

      <!-- 开启注解方式: -->

       <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>

     <!-- 自定义转换器 -->

        <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">

             <property name="converters">

             <list>

                 <bean class="cn.hd.comverter.DateConverter"></bean>

             </list>

             </property>

        </bean>  

    2

    public class DateConverter implements Converter<String, Date> {

    @Override

    public Date convert(String str) {

    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

    try {

    return sdf.parse(str);

    } catch (ParseException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    return null;

    }

       

      }

     B)第二种方式:

        @InitBinder

    public void initBinder(ServletRequestDataBinder binder) {

    binder.registerCustomEditor(Date.class, new CustomDateEditor(

    new SimpleDateFormat("yyyy-MM-dd"), true));

    }


    /**
    * @InitBinder会首先执行,将本类中所有属性是Date类型的,转换成自定义格式
    * @param binder
    */
    @InitBinder
    public void initBinder(WebDataBinder binder){
    SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
    sd.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(sd, false));

    }

  • 相关阅读:
    【mybatis】02-spring集成
    【Spring】xxAware
    【性能调优】Arthas
    【算法】其他算法(字典树Trie等)
    【多线程】JDK源码类图
    POJ-1251-Jungle Roads
    Prim算法模板
    敌兵布阵-线段树(1)
    hdu-1541-Stars (树状数组)
    母牛生小牛
  • 原文地址:https://www.cnblogs.com/joyous-day/p/6126157.html
Copyright © 2011-2022 走看看