davioooh davioooh - 7 months ago 31
Java Question

How to handle different date formats in Spring MVC controller?

Is it possible to handle different date format in a Spring MVC controller?

I know that setting something like this

protected void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
binder.registerCustomEditor(Date.class, new CustomDateEditor(
dateFormat, false));

I can handle
format, but what if i want to parse also dates in
format? Should I add multiple
s in my controller?


If at a time you receive only one format of date, then you could simply create one instance of DateFormat based on format

for example

Decide the format based on the input

DateFormat df = null;
    df = new SimpleDateFormat("dd/MM/yyyy")
    df = new SimpleDateFormat("yyyyMMddhhmmss")