SpringMVC自定义日期格式进行参数绑定

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010741376/article/details/46908979

配置文件里面先注册自定义转换器:

第一种配置:

	<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
	
<!-- 自定义日期参数绑定 -->
	<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
	      <!-- 转换器 -->
	        <property name="converters">
	           <list>
	              <bean class="com.web.util.DateConverter"></bean>
	           </list>
	        </property>
	</bean>

第二种配置:

	<!--注解适配器 -->
	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		 <property name="webBindingInitializer" ref="customBinder"></property> 
	</bean>
	
	<!-- 自定义webBinder -->
	<bean id="customBinder"
		class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
		<property name="conversionService" ref="conversionService" />
	</bean>
	<!-- conversionService -->
	<bean id="conversionService"
		class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<!-- 转换器 -->
		<property name="converters">
			<list>
				<bean class="com.web.util.DateConverter"/>
			</list>
		</property>
	</bean>




自定义转换器Converter:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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



public class DateConverter implements Converter<String,Date>{

	private static final List<String> formarts = new ArrayList<String>(4);
    static{
        formarts.add("yyyy-MM");
        formarts.add("yyyy-MM-dd");
        formarts.add("yyyy-MM-dd hh:mm");
        formarts.add("yyyy-MM-dd hh:mm:ss");
    }
	@Override
	public Date convert(String source) {
		 String value = source.trim();
	        if ("".equals(value)) {
	            return null;
	        }
	        if(source.matches("^\\d{4}-\\d{1,2}$")){ 
	            return parseDate(source, formarts.get(0));
	        }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")){
	            return parseDate(source, formarts.get(1));
	        }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")){
	            return parseDate(source, formarts.get(2));
	        }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
	            return parseDate(source, formarts.get(3));
	        }else {
	            throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
	        }
	}
    
	public Date parseDate(String dateStr,String format){
		Date date=null;
		DateFormat dateFormat=new SimpleDateFormat(format);
		try {
			date=dateFormat.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
		
	}
	public static void main(String[] args) {
		DateConverter date=new DateConverter();
		System.out.println(date.convert("2015-01-16"));
		
		
		
	}
	
}


上一篇:GerritCodeReview/docker-gerrit 安装记录


下一篇:Linux下查看文件编码,文件编码格式转换和文件名编码转换.