-----------------------------------DateConverter-------------------------------------------------
package com.jht.vcmsv1.converter;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
public class DateConverter extends DefaultTypeConverter {
private static final DateFormat[] ACCEPT_DATE_FORMATS = {
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"),
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),
new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("dd/MM/yyyy"),
new SimpleDateFormat("yyyy/MM/dd") }; //支持转换的日期格式
@Override
public Object convertValue(Map context, Object value, Class toType) {
if (toType == Date.class) { //浏览器向服务器提交时,进行String to Date的转换
String dateString = null;
String[] params = (String[])value;
dateString = params[0];//获取日期的字符串
for (DateFormat format : ACCEPT_DATE_FORMATS) {
try {
return format.parse(dateString);//遍历日期支持格式,进行转换
} catch(Exception e) {
continue;
}
}
return null;
}
else if (toType == Timestamp.class) { //浏览器向服务器提交时,进行String to Date的转换
String dateString = null;
String[] params = (String[])value;
dateString = params[0];//获取日期的字符串
for (DateFormat format : ACCEPT_DATE_FORMATS) {
try {
return new Timestamp(format.parse(dateString).getTime());//遍历日期支持格式,进行转换
} catch(Exception e) {
continue;
}
}
return null;
}
else if (toType == String.class) { //服务器向浏览器输出时,进行Date to String的类型转换
Date date = (Date)value;
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(date);//输出的格式是yyyy-MM-dd
}
return null;
}
}
------------------------------------xwork-conversion.properties----------- 根目录下------------------------
java.util.Date=com.jht.vcmsv1.converter.DateConverter