How to handle different date formats in Spring MVC controller?

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

I know that installing something like this

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

I can handle the dd/MM/yyyy format, but what if I want to parse dates in the yyyyMMddhhmmss format yyyyMMddhhmmss ? Should I add multiple CustomDateEditor to my controller?

+4
source share
6 answers

If you get only one date format, you can simply create one instance of DateFormat based on the format

, eg

Define a format based on input

 DateFormat df = null; if(recievedDate.indexOf("//")!=-1){ df = new SimpleDateFormat("dd/MM/yyyy") }else{ df = new SimpleDateFormat("yyyyMMddhhmmss") } 
+1
source

Inspired by Skip

 public class LenientDateParser extends PropertyEditorSupport { private static final List<String> formats = new ArrayList<String>(); private String outputFormat; static{ formats.add("dd-MM-yyyy HH:ss"); formats.add("dd/MM/yyyy HH:ss"); formats.add("dd-MM-yyyy"); formats.add("dd/MM/yyyy"); formats.add("dd MMM yyyy"); formats.add("MMM-yyyy HH:ss"); formats.add("MMM-yyyy"); formats.add("MMM yyyy"); } public LenientDateParser(String outputFormat){ this.outputFormat = outputFormat; } @Override public void setAsText(String text) throws IllegalArgumentException { if(StringUtils.isEmpty(text)) return; DateTime dt = null; for(String format : formats){ try{ dt = DateTime.parse(text, DateTimeFormat.forPattern(format)); break; }catch(Exception e){ if(log.isDebugEnabled()) log.debug(e,e); } } if(dt != null) setValue(dt.toDate()); } @Override public String getAsText() { Date date = (Date) getValue(); if(date == null) return ""; DateTimeFormatter f = DateTimeFormat.forPattern(outputFormat); return f.print(date.getTime()); } } 
+4
source

How about this one. the foregoing may soon disappear.

  public class MostLenientDateParser { private final List<String> supportedFormats; public MostLenientDateParser(List<String> supportedFormats) { this.supportedFormats = supportedFormats; } public Date parse(String dateValue) { for(String candidateFormat: supportedFormats) { Date date = lenientParse(dateValue, candidateFormat); if (date != null) { return date; } } throw new RuntimeException("tried so many formats, non matched"); } private Date lenientParse(String dateCandidate, String dateFormat) { try { return new SimpleDateFormat(dateFormat).parse(dateCandidate); } catch (Exception e) { return null; } } } 

This can also be referenced through Spring Converters through a CustomDateEditor implementation to bind form data.

+3
source

If you need it only in puntual cases, you can register a custom editor attached to the field in the form:

 DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", this.getLocale(context)); DateFormat dateTimeFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss SSS", this.getLocale(context)); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateTimeFormat, true)); binder.registerCustomEditor(Date.class, "name.of.input", new CustomDateEditor(dateTimeFormat, true)); 
+3
source

For others having the same question, if you are using spring 3, you can use awesome @DateTimeFormat (pattern = "dd-MM-yyyy") in your model field.

Just remember to register the conversion service with your org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

You can have as many as @DateTimeFormat in the same bean.

+2
source

It is not a great idea to have soft date formats when working with multiple locales. A date like 10/11/2013 will be correctly analyzed with both dd / MM / YYYY and MM / dd / YYYY

+1
source

All Articles