davioooh davioooh - 1 year ago 96
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?

Answer Source

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")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download