SpringMvc接收日期参数

╰半夏微凉° 2022-06-02 09:28 346阅读 0赞

首先引入jodatime jar

  1. <dependency>
  2. <groupId>joda-time</groupId>
  3. <artifactId>joda-time</artifactId>
  4. <version>2.9.9</version>
  5. </dependency>

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

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

传入值?time=2017-12-12

也可通过实体接收

  1. @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

发表评论

表情:
评论列表 (有 0 条评论,346人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringMVC-参数接收

    > 逐个参数接收  只要保证请求参数名与该请求处理方法的参数名相同即可  SpringMVC 绑定请求参数是自动实现的,但是要想使用,必须遵循使用要求: 1、如果是基本类