Setting Default DateTimeFormat Annotations in Spring

I know that the DateTimeFormat annotation can be defined in the Date properties of the Model class in Spring, but is there a way to define a default dateTimeFormat annotation for all dates in Spring classes?

@DateTimeFormat (pattern = "MM / dd / YYYY")
Private deliveryDate

This will save me from annotating each date field. It will also make it easier for me to change the format for wide format printing of the application later.

0
source share
2 answers

As mentioned in kuhajeyan, you can use a binder, but not on the controller, but on the board of the controller, which 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 code } } 
+3
source

in your controller

 @InitBinder protected void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/YYYY"); binder.registerCustomEditor(Date.class, new CustomDateEditor( dateFormat, false)); } 
+1
source

Source: https://habr.com/ru/post/1270437/


All Articles