Spring MVC设置全局DateTimeFormat

Can*_*Can 5 java spring spring-mvc

我希望我的controllers解析日期的RequestParamsPathVariables按照标准格式.

是否可以在@DateTimeFormat不对每个@RequestParam单独注释的情况下设置应用程序范围?

Dee*_*arg 2

您可以在控制器级别执行此操作。添加@initBinder您的控制器,它将根据给定的格式化程序格式化日期。

@InitBinder
public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(false);

    // true passed to CustomDateEditor constructor means convert empty String to null
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
Run Code Online (Sandbox Code Playgroud)