Spring @RequestParam日期格式

时间:2015-04-15 17:40:13

标签: java spring annotations http-request-parameters

如何使用注释格式化传入的@RequestParam?表单以MM / DD / YYYY格式发送日期,控制器未将其作为有效Date提取。

@RequestMapping(params="/updateDate")
public @ResponseBody String updateDate(HttpServletRequest request
        , @RequestParam Integer productId
        , @RequestParam Date dateReceived) {
    // Code here...
}

1 个答案:

答案 0 :(得分:12)

使用Spring注释DateTimeFormat

@RequestMapping(params="/updateDate")
public @ResponseBody String updateDate(HttpServletRequest request
        , @RequestParam Integer productId
        , @RequestParam @DateTimeFormat(pattern="MM/dd/yyyy") Date dateReceived) {
    // Code here...
}