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?
Cause joda-time is deprecated.
Joda-Time provides support for multiple calendar systems and the full range of time-zones. The Chronology and DateTimeZone classes provide this support. Joda-Time defaults to using the ISO calendar system, which is the de facto civil calendar used by the world.
Joda-Time is an API created by joda.org which offers better classes and having efficient methods to handle date and time than classes from java. util package like Calendar, Gregorian Calendar, Date, etc. This API is included in Java 8.0 with the java.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With