SpringMVC自定义时间类型转换

springmvc中Controller在接收前端传递参数的时候

public Map<String ,String> save(T bean)

 我们一般是这样来定义接收的bean对象,其中bean对象中的时间类型,我们会定义为Date,

那么在前端进行参数传递的时候,前端传递的时间如:2017-01-18是一个字符串和我们bean对象不匹配,那么就需要进行转换。

现在我这里定义了一个

@Controller
@RequestMapping(value = "/base")
public abstract class BaseController<T>
BaseController为Controller继承的一个父类来统一处理

    代码如下:

 @InitBinder
protected void initBinder(WebDataBinder binder) {

        //自定义全局时间接收转换处理
binder.registerCustomEditor(Date.class, new DateEditor());

//        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
//            @Override
//            public void setAsText(String value) {
//                 setValue(new Date(Long.valueOf(value)));
//            }
//        });

}

   其中注释掉部分为传统写法,我这里,自定义了一个类型来接处理时间类型

   自定义类:DateEditor

  

package org.moon.framework.util.date;

import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.moon.framework.util.exception.FrameworkException;
import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


/**
 * 自定义SpringMVC 时间格式转义
 */
public class DateEditor extends PropertyEditorSupport {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    /**

     * Parse the Date from the given text, using the specified DateFormat.

     */
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        try {
            // Treat empty String as null value.
            if (StringUtils.isBlank(text)) {
                setValue(null);
                return;
            }
            //如果时间类型值为直接是数字的情况  12位数字
            if(text.matches("^[0-9]{12}.*")) {
                Date date  = new Date(Long.valueOf(text));
                setValue(date);
            } else {
                setValue(this.dateAdapter(text));
            }
        } catch (FrameworkException ex) {
            ex.printStackTrace();
            logger.error("时间类型转换出错....:"+ex.getMessage());
        }
    }

    /**

     * Format the Date as String, using the specified DateFormat.

     */
    @Override
    public String getAsText() {
        Date value = (Date) getValue();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return (value != null ? dateFormat.format(value) : "");

    }



    public static Date dateAdapter(String dateStr) throws FrameworkException {
        Date date = null;
        String temp=dateStr;//缓存原始数据
        if(dateStr.contains("CST")){
            date = new Date(dateStr);
            return date;
        }
        if(StringUtils.isBlank(dateStr)){
            return date;
        }
        dateStr = dateStr.replace("年", "-").replace("月", "-").replace("日", "").replaceAll("/", "-").replaceAll("\\.", "-").trim();
        String fm="";
        //确定日期格式
        if(Pattern.compile("^[0-9]{4}-[0-9]{2}-[0-9]{2}.*").matcher(dateStr).matches()){
            fm = "yyyy-MM-dd";
        } else if(Pattern.compile("^[0-9]{4}-[0-9]{1}-[0-9]+.*||^[0-9]{4}-[0-9]+-[0-9]{1}.*").matcher(dateStr).matches()){
            fm = "yyyy-M-d";
        } else if(Pattern.compile("^[0-9]{2}-[0-9]{2}-[0-9]{2}.*").matcher(dateStr).matches()){
            fm = "yy-MM-dd";
        } else if(Pattern.compile("^[0-9]{2}-[0-9]{1}-[0-9]+.*||^[0-9]{2}-[0-9]+-[0-9]{1}.*").matcher(dateStr).matches()){
            fm = "yy-M-d";
        }
        //确定时间格式
        if(Pattern.compile(".*[ ][0-9]{2}").matcher(dateStr).matches()){
            fm+= " HH";
        }else if(Pattern.compile(".*[ ][0-9]{2}:[0-9]{2}").matcher(dateStr).matches()){
            fm+= " HH:mm";
        }else if(Pattern.compile(".*[ ][0-9]{2}:[0-9]{2}:[0-9]{2}").matcher(dateStr).matches()){
            fm+= " HH:mm:ss";
        }else if(Pattern.compile(".*[ ][0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{0,3}").matcher(dateStr).matches()){
            fm+= " HH:mm:ss:sss";
        }
        if(StringUtils.isNotBlank(fm)){
            date = DateUtilServer.stringToDate(dateStr,fm);
        }
        return date;
    }

}

  其中if(text.matches("^[0-9]{12}.*"))是对使用hibernate时间类型为乐观锁的处理

 希望能够帮助到你们 

猜你喜欢

转载自liaoyue11.iteye.com/blog/2353997