import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//属性转换工具类
public class StringToDateConverter implements Converter<String,Date> {
@Override
public Date convert(String source) {
//日期转换方法,把字符串,转换为时间类型
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
Date dt = sdf.parse(source);
return dt;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
在springMVC配置文件里,写类型转换工厂bean,并且注解引用
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<!--扫描包:创建web层对象-->
<context:component-scan base-package="com.itheima.controller"></context:component-scan>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--注解驱动,并且引用工厂类对象-->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!--静态资源放行-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--<mvc:resources mapping="/css/*" location="/css/"></mvc:resources>-->
<!--<mvc:resources mapping="/img/*" location="/img/"></mvc:resources>-->
<!--<mvc:resources mapping="/plugins/*" location="/plugins/"></mvc:resources>-->
<!--创建类型转换器工厂类对象-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.itheima.converter.StringToDateConverter"></bean>
</set>
</property>
</bean>
</beans>
UUID:
public class UUIDUtil {
public static String getId(){
//得到随机的32为字符串,去掉中间的 - ,并把所有字符转换为大写
String s = UUID.randomUUID().toString().replace("-", "").toUpperCase();
return s;
}