Firefox和Struts2在Date上干架 firefoxDatestruts2
程序员文章站
2024-03-21 20:29:46
...
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 { /* * http://blog.163.com/ztxz_179/blog/static/10520599520122575940933/ * 在classpath根下创建 xwork-conversion.properties文件 * 增加配置项:java.util.Date=com.sohu.video.ad.struts.converter.DateConverter * */ private static final String FORMATDATE = "yyyy-MM-dd"; private static final String FORMATTIME = "yyyy-MM-dd HH:mm:ss"; @SuppressWarnings("unchecked") @Override public Object convertFromString(Map context, String[] values, Class toClass) { if (values == null || values.length == 0) { return null; } // 有时分秒的要先转换 SimpleDateFormat sdf = new SimpleDateFormat(FORMATTIME); Date date = null; String dateString = values[0]; if (dateString != null) { try { date = sdf.parse(dateString); } catch (ParseException e) { date = null; } if (date == null) { sdf = new SimpleDateFormat(FORMATDATE); try { date = sdf.parse(dateString); } catch (ParseException e) { date = null; } } } return date; } @SuppressWarnings("unchecked") @Override public String convertToString(Map context, Object o) { if (o instanceof Date) { SimpleDateFormat sdf = new SimpleDateFormat(FORMATTIME); return sdf.format((Date) o); } return ""; } }