首页 > 其他分享 >SpringMvc接收日期参数

SpringMvc接收日期参数

时间:2023-02-01 14:37:11浏览次数:34  
标签:dateFormat SpringMvc binder new SimpleDateFormat 日期 time query 接收


首先引入jodatime jar

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>

Controller方法通过@DateTimeFormat注解来接收参数

@RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time

传入值?time=2017-12-12

也可通过实体接收

@InitBinder("query")
public void initBinderQuery(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
binder.setFieldDefaultPrefix("query.");
}
@InitBinder("query")
public void initBinderQuery(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
binder.setFieldDefaultPrefix("query.");
}

传入值?query.time=2017-12-12


标签:dateFormat,SpringMvc,binder,new,SimpleDateFormat,日期,time,query,接收
From: https://blog.51cto.com/u_15950441/6031553

相关文章