package cn.sxx.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
public class DateConverter extends StrutsTypeConverter {
@Override
public Object convertFromString(Map map, String[] vals, Class class1) {
Date date = null;
if(vals != null && vals.length > 0){
String dateStr = vals[0];
if(class1 == Date.class){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
return date;
}
@Override
public String convertToString(Map map, Object obj) {
String result = "";
if(obj != null && obj.getClass() == Date.class){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
result = sdf.format(obj);
}
return result;
}
}
配置文件:xwork-conversion.properties的内容为:
java.util.Date=DateConverter 的全路径
原文:https://www.cnblogs.com/Mr-Dxin/p/11090624.html