Date type conversion in springmvc

// implement the type conversion interface 
package cn.bdqn.stusystem.util;

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

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

public class DateConverter implements Converter<String, Date>{

    @Override
    public Date convert(String arg0) {
        SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sm.parse(arg0);
        } catch (ParseException e) {
            
        }
        return null;
    }

}

// Configure in springmvc
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <property name="converters">
        <set>
            <bean class="cn.bdqn.stusystem.util.DateConverter"/>
        </set>
    </property>
</bean>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325341594&siteId=291194637