[英]Making a getAsText() with Date in PropertyEditorSupport and @Initbinder
I need to convert date with pattern "yyyy-MM-dd" to String format "dd.MM.yyyy".我需要将模式“yyyy-MM-dd”的日期转换为字符串格式“dd.MM.yyyy”。 Now i have this.现在我有了这个。 Method getAsText() doesn't work meanwhile setAsText working.方法 getAsText() 在 setAsText 工作的同时不起作用。 What i do wrong?我做错了什么?
DateEditor.java日期编辑器.java
@Component
public class DateEditor extends PropertyEditorSupport {
@Override
public void setAsText(String value) {
try {
setValue(new SimpleDateFormat("yyyy-MM-dd").parse(value));
} catch (Exception ex) {
setValue(null);
}
}
@Override
public String getAsText() {
String sdf = "";
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(true);//-
Date date = dateFormat.parse((String) getValue());
sdf = new SimpleDateFormat("dd.MM.yyyy").format(date);
System.out.println(sdf);
} catch (ParseException p) {}
return sdf;
}
}
Initbinder初始化绑定器
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
dataBinder.registerCustomEditor(Date.class,new DateEditor());
}
nothing showing in the cach block try to see what happend when ParseException is thrown缓存块中没有显示任何内容尝试查看抛出 ParseException 时发生了什么
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.