Setting default DateTimeFormat Annotation in Spring

前端 未结 2 483
别跟我提以往
别跟我提以往 2021-01-17 01:47

I know that a DateTimeFormat annotation can be defined on Date properties of a Model class in Spring but is there any way of defining a default dateTimeFormat annotation for

相关标签:
2条回答
  • 2021-01-17 02:22

    in your controller

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/YYYY");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(
                dateFormat, false));
    }
    
    0 讨论(0)
  • 2021-01-17 02:28

    As kuhajeyan mentioned, you can use binder, but not on a controller, rather on controller advice, that will apply it globally:

    @ControllerAdvice
    public class GlobalDateBinder {
    
     /* Initialize a global InitBinder for dates*/
    
     @InitBinder
     public void binder(WebDataBinder binder) {
      // here you can use kuhajeyan's code
     }
    }
    
    0 讨论(0)
提交回复
热议问题