I want my controllers
to parse dates in RequestParams
and PathVariables
according to a standard format.
Is it possible to set an application-wide @DateTimeFormat
without annotating every @RequestParam
individually?
You can do it at the controller level. Add @initBinder
in your controller and it will format the date according to the given formatter.
@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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With