SpringMVC后台使用对象接受参数字符串转日期

在springMVC配置文件中加入:
<bean id="dateConvert" class="com.iomp.util.DateConvert"/>
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <ref bean="dateConvert"/>
            </set>
        </property>
    </bean>
    <mvc:annotation-driven conversion-service="conversionService"/>
对应实现类:
package com.iomp.util;

import org.springframework.core.convert.converter.Converter;

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

/**
  * 对象方式接受页面参数,字符串转日期格式
  * ClassName: DateConvert.
  * User: lyc
  * Date: 2017/9/16
  * Time: 18:29
 */

public class DateConvert implements Converter<String, Date> {
    @Override
    public Date convert(String stringDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        try {
            date = simpleDateFormat.parse(stringDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

}

 

posted @ 2017-09-20 10:45  super超人  阅读(2401)  评论(0编辑  收藏  举报