package com.tools.converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.sql.Date;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
import com.opensymphony.xwork2.XWorkException;
public class DateConverter extends StrutsTypeConverter {
public static final String DATE_FORMAT = "yyyy/MM/dd";
@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
Date[] result = null;
if ((values != null) && (values.length >= 1)) {
result = new Date[values.length];
for (int i=0; i < values.length; i++){
result[i] = convertToDate(values[i]);
}
}
if (result != null && values.length == 1) {
return result[0];
} else {
return result;
}
}
@Override
public String convertToString(Map context, Object o) {
String result = null;
if (o instanceof Date) {
result = new SimpleDateFormat(DATE_FORMAT).format(o);
}
return result;
}
private Date convertToDate(String value) {
Date result = null;
if (value == null || value.isEmpty()) {
return result;
}
try {
result = new Date(new SimpleDateFormat(DATE_FORMAT).parse(value).getTime());
} catch (ParseException e) {
throw new XWorkException("Could not parse date", e);
}
return result;
}
}
zhangchuanfu
- 粉丝: 7
- 资源: 10
最新资源
- xxhash-3.0.0-cp37-cp37m-win32.whl.rar
- xxhash-3.0.0-cp38-cp38-win_amd64.whl.rar
- xxhash-3.0.0-cp39-cp39-win_amd64.whl.rar
- xxhash-3.0.0-cp310-cp310-win_amd64.whl.rar
- xxhash-3.0.0-cp39-cp39-win32.whl.rar
- xxhash-3.0.0-cp311-cp311-win32.whl.rar
- xxhash-3.0.0-cp310-cp310-win32.whl.rar
- xxhash-3.0.0-cp311-cp311-win_amd64.whl.rar
- xxhash-3.0.0-pp38-pypy38_pp73-win_amd64.whl.rar
- xylib_py-1.5.0-cp35-cp35m-win32.whl.rar
- xylib_py-1.5.0-cp35-cp35m-win_amd64.whl.rar
- xylib_py-1.6.1-cp36-cp36m-win_amd64.whl.rar
- xylib_py-1.6.1-cp38-cp38-win32.whl.rar
- xylib_py-1.6.1-cp38-cp38-win_amd64.whl.rar
- xylib_py-1.6.1-cp37-cp37m-win32.whl.rar
- yappi-0.99-cp34-cp34m-win32.whl.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
前往页