First of all ... Im relatively new in Spring, I use spring 3.x and I DONT LIKE SPRING\'S XML CONFIGURATION FILES ... I dont want for every refactoring I do, to run into XML
I am not sure if this works in Spring 3 but this is the solution for Spring 4:
@Configuration
@EnableWebMvc
class WebMvcContext extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new DateConverter("yyyy-MM-dd HH:mm:ss"));
//registry.addConverter(anotherConverter);
}
}
DateConverter is a custom converter:
public class DateConverter implements Converter{
private static final Logger LOGGER = LoggerFactory.getLogger(DateConverter.class);
private final String dateFormat;
private final SimpleDateFormat formatter;
public DateConverter(String dateFormatPattern) {
this.dateFormat = dateFormatPattern;
this.formatter = new SimpleDateFormat(dateFormatPattern);
}
@Override
public Date convert(String source) {
Date date = null;
try {
date = formatter.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}