package com.hgd.ssm.utils;
import org.springframework.core.convert.converter.Converter;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @auther 黄国栋
* @data 2020-06-29 14:39
* @since
*/
public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String s) {
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try{
return simpleDateFormat.parse(s);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--配置处理器适配器和处理器映射器-->
<mvc:annotation-driven conversion-service="conversionService"/>
<!--配置转换器-->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.hgd.ssm.utils.DateConverter"></bean>
</set>
</property>
</bean>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--扫描controller-->
<context:component-scan base-package="com.hgd.ssm.controller"/>
<!--文件上传-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--上传文件的大小-->
<property name="maxUploadSize" value="100000000"></property>
</bean>
</beans>
<hr>
<a href="user/deleteUser?birthday=2018-01-01">根据日期删除用户(String)</a>
<hr>
<a href="user/deleteUser2?birthday=2018-01-01 11:12:00">根据日期删除用户(Data)</a>
//根据日期删除用户(String)
@RequestMapping("deleteUser")
public String deleteUser(String birthday){
System.out.println("birthday = " + birthday);
return "success";
}
//根据日期删除用户(Data)
@RequestMapping("deleteUser2")
public String deleteUser2(Date birthday){
System.out.println("birthday = " + birthday);
return "success";
}