SpringMVC-07-日期型参数传递
1、@DateTimeFormat 注解
@DateTimeFormat 注解用于设置传入的请求参数的日期格式,注解在控制器方法的形参前
@RequestMapping("dateParam")
@ResponseBody
public String dateParam(Date date,
@DateTimeFormat(pattern = "yyyy-MM-dd") Date date1,
@DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss SS") Date date2) {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SS");
System.out.println("参数传递 date = " + f.format(date));
System.out.println("参数传递 date1 = " + f.format(date1));
System.out.println("参数传递 date2 = " + f.format(date2));
return f.format(date) + "\n" + f.format(date1) + "\n" + f.format(date2);
}
不带 @DateTimeFormat 注解的参数也可以装换成 Date,格式为 yyyy/MM/dd HH:mm:ss
上面的示例用 get 请求传入参数可以用 url:localhost/dateParam?date=2016/3/7 15:10:20&date1=2017-8-9&date2=2018/10/7 18:20:30 123
2、类型装换器
Spring 中存在一个 Converter 接口,注解在 SpringMVC配置类上的 @EnableWebMvc 注解的功能之一,是根据类型匹配对应的类型转换器
@FunctionalInterface
public interface Converter<S, T> {
@Nullable
T convert(S var1);
}