Controller层接收表单提交的数据时,Sring转换为Date

@InitBinder
public void initBinder(WebDataBinder binder) throws Exception {
// 注册自定义的属性编辑器
//DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//CustomDateEditor dateEditor = new CustomDateEditor(df, true);
// 表示如果命令对象有Date类型的属性,将使用该属性编辑器进行类型转换
//binder.registerCustomEditor(Date.class, dateEditor);
binder.registerCustomEditor(Date.class, new MyCustomDateEditor());

}




/**
 * 重写spring日期转换器,自定义日期转换器
 * @author Robert_Zhan
 */
public class MyCustomDateEditor extends PropertyEditorSupport {



@Override
@SuppressWarnings("static-access")
public void setAsText(String text) throws IllegalArgumentException {
try{
if (!StringUtils.hasText(text)) {
setValue(null);
}else {
setValue(this.dateAdapter(text));
}
}catch (Exception ex) {
ex.printStackTrace();
}
}


/**
     * 字符串转日期适配方法
     * @param dateStr 日期字符串
     */
    @SuppressWarnings("deprecation")
public static Date dateAdapter(String str)  {
   Date date = null;
   
   try {
    if(StringUtils.isEmpty(str)){
    //判断是不是日期字符串,如Wed May 28 08:00:00 CST 2014
       if(str.contains("CST")){
        date=new Date(str);
       }else{
        str = str.replace("年", "-").replace("月", "-").replace("日", "").replaceAll("/", "-").replaceAll("\\.", "-").trim();
        String format = "";
        //日期格式匹配
           if(Pattern.compile("^[0-9]{4}-[0-9]{2}-[0-9]{2}.*").matcher(str).matches()){
            format = "yyyy-MM-dd";
           }else if(Pattern.compile("^[0-9]{4}-[0-9]{1}-[0-9]+.*||^[0-9]{4}-[0-9]+-[0-9]{1}.*").matcher(str).matches()){
            format = "yyyy-M-d";
           }else if(Pattern.compile("^[0-9]{2}-[0-9]{2}-[0-9]{2}.*").matcher(str).matches()){
               format = "yy-MM-dd";
           }else if(Pattern.compile("^[0-9]{2}-[0-9]{1}-[0-9]+.*||^[0-9]{2}-[0-9]+-[0-9]{1}.*").matcher(str).matches()){
            format = "yy-M-d";
           }
           
           //时间格式匹配
           if(Pattern.compile(".*[ ][0-9]{2}").matcher(str).matches()){
            format += " HH";
           }else if(Pattern.compile(".*[ ][0-9]{2}:[0-9]{2}").matcher(str).matches()){
            format += " HH:mm";
           }else if(Pattern.compile(".*[ ][0-9]{2}:[0-9]{2}:[0-9]{2}").matcher(str).matches()){
            format += " HH:mm:ss";
           }else if(Pattern.compile(".*[ ][0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{0,3}").matcher(str).matches()){
            format += " HH:mm:ss:sss";
           }
           
           //判断当前格式化内容是否为空
           if(!StringUtils.isEmpty(format)){
            date = new SimpleDateFormat(format).parse(str);
           }else{
            throw new  RuntimeException("参数字符串"+str+",格式存在问题,无法进行日期转换!");
           }
       }
       }
} catch (Exception e) {
throw new  RuntimeException("参数字符串"+str+"无法被转换为日期格式!");
}
   return date;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值