我的应用程序正在使用Play 2.3.7。
我有一个需要日期特殊格式化程序的表单。因此,我在创建表单并呈现视图的控制器方法中执行此操作:
Formatters.register(Date.class, new SimpleFormatter<Date>() {
private SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
@Override
public Date parse(String input, Locale locale) throws ParseException {
try {
return formatter.parse(input);
} catch (ParseException e) {
// We catch the error because it should be transparent for the user
Logger.error(e.getLocalizedMessage(), e);
return null;
}
}
@Override
public String print(Date input, Locale locale) {
return formatter.format(input);
}
});
问题是此格式化程序已全局应用于我的所有应用程序。我的其他表格使用的是其他日期格式:dd / MM / yyyy HH:mm:ss(法语格式)。那么,如何才能将格式化程序仅应用于一种形式,或者至少取消注册格式化程序?
谢谢
您可以使用一种Formats
注释,并如下所示对表单字段进行注释:
public class SampleForm {
@Formats.DateTime(pattern = "yyyy-MM-dd HH:mm:ss")
private Date date;
...
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句