SpringMvc日期转换的三种方式


 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;
}

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


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

或者点个赞大笑














  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值