SpringMvc日期转换的三种方式

来源:互联网 发布:里约奥运会数据库 编辑:程序博客网 时间:2024/05/20 06:56


 1 使用@InitBinder注解的方式


这种方式比较简单只要在Controller中配置@InitBinder完成


DateController.class

 @InitBinder
       public void initBinder(WebDataBinder binder){

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

          //第一个参数为需要转换成的类型   第二个参数为转换的格式

           binder.registerCustomEditor(Date.class, new CustomDateEditor(format, true));
       }


这样即可完成日期的转换  但是有局限  日期格式只能是yyyy-MM-dd这种形式 如果需要自定义类型则需要重写第二个参数

如下

这是我自定义的日期编辑器MyDateEditor.java

package method;

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

import org.springframework.beans.propertyeditors.PropertiesEditor;

public class MyDateEditor extends PropertiesEditor{
     @Override
    public void setAsText(String text) throws IllegalArgumentException {
         Date date=null;
          SimpleDateFormat sdf = getDateFormat(text);
          try {
              date = sdf.parse(text);
              setValue(date);
          } catch (ParseException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
    }

//使用正则表达式 可以更加灵活控制输入的格式类型

     public SimpleDateFormat  getDateFormat(String source){
         SimpleDateFormat sf = new SimpleDateFormat();

//代表格式为yyyy-MM-dd

         if(Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", source)){
             sf = new SimpleDateFormat("yyyy-MM-dd");
         }

//代表格式为yyyy/MM/dd

         if(Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", source)){
             sf = new SimpleDateFormat("yyyy/MM/dd");
         }

//代表格式为yyyyMMdd

   if(Pattern.matches("^\\d{4}\\d{2}\\d{2}$", source)){
             sf = new SimpleDateFormat("yyyyMMdd");
         }
         return sf;
     }
}

最后把之前的Spring自带的日期编辑器CustomDateEditor改成自已的日期编辑器就完成了

 @InitBinder
       public void initBinder(WebDataBinder binder){
           binder.registerCustomEditor(Date.class, new MyDateEditor());
       }


这样就完成了第一种方式的日期转换  能够更加灵活控制用户输入日期的格式


2.实现Converter接口 支持把任意的各类转换成另一个另外一个类型


DateConverter.class

public class StringToDateConverter implements Converter<String, Date> {

    @Override
    public Date convert(String source) {
        Date date = null;
        SimpleDateFormat sdf = getDateFormat(source);
        try {

         //字符串转换成日期格式

            date = sdf.parse(source);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;
    }
  //利用正则表达式控制日期的输入格式
    public SimpleDateFormat getDateFormat(String source) {
        SimpleDateFormat sf = new SimpleDateFormat();
        if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", source)) {
            sf = new SimpleDateFormat("yyyy-MM-dd");
        }
        if (Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", source)) {
            sf = new SimpleDateFormat("yyyy/MM/dd");
        }
        if (Pattern.matches("^\\d{4}\\d{2}\\d{2}$", source)) {
            sf = new SimpleDateFormat("yyyyMMdd");
        }
        return sf;
    }

}


接下来我们还需要在xxx-servlet.xml中声明该转换器 通知Spring容器我们需要日期转换

需要三个步骤

//1.注册我们之前自定义的日期类型转换器   
  <bean name="dateConverter"  class="date.StringToDateConverter"></bean>

//2.  註冊转换器服务对象 可能包含多个转换器
 <bean id="conversionservice"  class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
    <set >
    <ref bean="dateConverter"/>
    </set>
    </property>
    </bean> 
  //3.注册mvc注解驱动  注入转换器服务对象
    <!-- <mvc:annotation-driven conversion-service="conversionservice"/> -->

这样就完成了第二种方式的日期类型转换。



3.使用@DateTimeFormat注解  这也是最简单 最容易操作的一种方式 我们只需要直接在属性中使用注解声明需要转换的日期格式即可 代码如下

public class People {
  private String name;
  private int age;
  //注入自己需要转换的日期格式
  @DateTimeFormat(pattern="yyyy-MM-dd")
  private Date birthday;
}

这样即可完成日期的转换   是不是很操作很简单


如有不足请各位大神多多指教

或者点个赞大笑














原创粉丝点击