今天碰到一个Timestamp类型的日期,javabean 用的是Timestamp类型的时间,因为要获取数据库的时分秒,数据库是date类型的时间 在页面传入action的处理过程中出现了时间转换错误
具体的解决办法如下
package com.safein.util;

import java.sql.Timestamp;

import org.apachemons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;

/**
* Timestamp日期类型转换
* @author julong
* @date 2016-7-13 上午09:47:20
*/
public class TimestampConverter implements Converter<String,Timestamp>{

    @Override
    public Timestamp convert(String arg0) {
        // TODO Auto-generated method stub
        if(StringUtils.isNotEmpty(arg0.trim())){
            Timestamp timestamp = Timestamp.valueOf(arg0);
            return timestamp;
        }
    return null;

    }

}
在spring配置文件如下
<!-- 时间转换拦截器 -->
<mvc:annotation-driven conversion-service="converterUtil" />
<bean id="converterUtil" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.safein.util.TimestampConverter" />
</list>
</property>
</bean>
这样就能完美解决此问题了,特此一记

更多推荐

spring mvc 中Timestamp 日期的转换