package com.hdaccp.jboa.util;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
import com.opensymphony.xwork2.conversion.TypeConversionException;
public class MyDateConverter extends StrutsTypeConverter{
private final DateFormat[] dfs={
new SimpleDateFormat("yyyy年MM月dd日"),
new SimpleDateFormat("yyyy-MM-dd"),
};
@Override
public Object convertFromString(Map context, String[] values, Class toType) {
String dateStr=values[0];
for(int i=0;i<dfs.length;i++){
try{
return dfs[i].parse(dateStr);
}catch (Exception e) {
continue;
}
}
throw new TypeConversionException();
}
@Override
public String convertToString(Map context, Object object) {
Date date=(Date)object;
return new SimpleDateFormat("yyyy-MM-dd").format(date);
}
}
src下创建xwork-conversion.properties配置文件,内容如下:
java.util.Date=com.hdaccp.jboa.util