SpringMvc接收日期参数

来源:互联网 发布:a卡csgo优化设置 编辑:程序博客网 时间:2024/06/01 10:34

首先引入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.");}

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

原创粉丝点击