struts2默认时间格式是yyyy-MM-dd,要使用yyyyMMdd格式就要写格式转换器 局部:
第一步:写一个时间类型转换器 继承DefaultTypeConverter
[mw_shl_code=java,true]package utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
/**
* @author Delver_Si
*
*/
public class DateConvert extends DefaultTypeConverter {
@Override
public Object convertValue(Map<String, Object> context, Object value,
Class toType) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
try {
if(toType==Date.class){
String[] params = (String[]) value;
return dateFormat.parseObject(params[0]);//数组中的第一个元素即是要转换的
}else if (toType==String.class) {
Date date = (Date) value;
return dateFormat.format(date);
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
[/mw_shl_code]
在action同包下建立如下格式配置文件,-conversion.properties前是Action名字,属性后面是转换器的全路径
全局: 配置文件做如下修改,放置与src目录下
|