Java 如何使SpringJoda时间格式化程序在非标准地区工作?
我正在使用Spring3.1和开发一个多语言应用程序 假设我有一个命令对象,如下所示:Java 如何使SpringJoda时间格式化程序在非标准地区工作?,java,spring,spring-mvc,jodatime,Java,Spring,Spring Mvc,Jodatime,我正在使用Spring3.1和开发一个多语言应用程序 假设我有一个命令对象,如下所示: private class MyCommand { private LocalDate date; } 当我向英国或美国地区提出请求时,它可以正确解析并绑定日期,而不会出现任何问题,分别使用相应的日期格式,例如2013年10月21日和2013年10月21日。 但是,如果我有一些地区,比如格鲁吉亚语新地区(“ka”)它不绑定有效日期2014年10月21日。因此,我需要挂接Spring格式化程序,以便能
private class MyCommand {
private LocalDate date;
}
当我向英国或美国地区提出请求时,它可以正确解析并绑定日期
,而不会出现任何问题,分别使用相应的日期格式,例如2013年10月21日和2013年10月21日。
但是,如果我有一些地区,比如格鲁吉亚语
新地区(“ka”)
它不绑定有效日期2014年10月21日。因此,我需要挂接Spring格式化程序,以便能够为每个语言环境提供自己的格式。我有一个bean,它可以从区域设置解析日期格式。你能给我指出正确的方向吗?我怎样才能做到这一点 您必须实现自己的org.springframework.format.Formatter
范例
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
}
}
公共类DateFormatter实现格式化程序{
公共字符串打印(日期属性、区域设置){
//您的代码在此显示
DateFormat df=DateFormat.getDateInstance(DateFormat.SHORT,LocaleContextHolder.getLocale());
字符串输出=df.格式(日期);
返回;
}
公共日期解析(字符串源、区域设置)
//您的代码将在此处解析字符串
}
}
在spring配置中:
<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"/>
希望它能帮助你