Reputation: 4426
I am developing a multilingual application using Spring 3.1 and Joda-Time.
Let's imagine I have a command object like this:
private class MyCommand {
private LocalDate date;
}
When I request with UK or US locales it can parse correctly and bind date
without any problems using corresponding dates format e.g. 21/10/2013 and 10/21/13 respectively.
But if I have some locales like georgian new Locale("ka")
it doesn't bind valid date 21.10.2014. So I need to hook into Spring formatters to be able to provide my own formats per locale. I have a bean which can resolve date format from the locale. Can you please point me into right direction how can I accomplish this?
Upvotes: 6
Views: 1001
Reputation: 752
You have to implement your own org.springframework.format.Formatter
Example
public class DateFormatter implements Formatter<Date> {
public String print(Date property, Locale locale) {
//your code here for display
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, LocaleContextHolder.getLocale());
String out = df.format(date);
return out;
}
public Date parse(String source, Locale locale)
// your code here to parse the String
}
}
In your spring config :
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" >
<property name="formatterRegistrars">
<set>
</set>
</property>
<property name="converters">
<set>
</set>
</property>
<property name="formatters">
<set>
<bean class="com.example.DateFormatter" />
</set>
</property>
</bean>
<mvc:annotation-driven conversion-service="conversionService"/>
Hope it helps you!
Upvotes: 2