在tomcat6上面本来日期时间可以正常显示,但运行一段时间后,所有的<s:date 输出的内容变为: 2010-01-01 00:00:00,不知是什么原因啊。

解决方案 »

  1.   

    public class DateConverter extends DefaultTypeConverter {
    private static final DateFormat datetimeFormat = new SimpleDateFormat(
    "yyyy-MM-dd HH:mm:ss"); @Override
    public Object convertValue(Map context, Object value, Class toType) {
    if (toType == String.class) {
                return datetimeFormat.format(value);
            }
            else if (toType == Date.class) {  
                datetimeFormat.setLenient(false);   
                try {   
                    String[] s = (String[]) value;
                    if(StringUtil.isNullOrEmpty(s[0])) return "";
                    return datetimeFormat.parse(s[0]); 
                } catch (ParseException e) {   
                    System.out.println("Error:" + e);   
                    throw new TypeConversionException("Invalid conversion");   
                }
            }
            return null;   
        }
    }后在xwork-conversion.properties配置:
    java.util.Date=....DateConverter
      

  2.   

    public class DateConverter extends DefaultTypeConverter { private DateFormat[] dateFormat = { // 日期格式化器
    new SimpleDateFormat("yyyy-MM-dd HH:mm:ssss"), // 格式如2010-07-24 16:18:40.170
    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), // 格式如2010-07-24 16:18:40.170
    new SimpleDateFormat("yyyy-MM-dd HH:mm"), // 格式如2010-07-24 16:18:40.170
    new SimpleDateFormat("yyyy-MM-dd HH"), // 格式如2010-07-24 16:18:40.170
    new SimpleDateFormat("yyyy-MM-dd"), // 格式如2008-08-08
    new SimpleDateFormat("yyyy/MM/dd"), // 格式如2008/08/08
    new SimpleDateFormat("yy-MM-dd"), }; // 格式如08-08-08

    private DateFormat[] timeFormat = { // 时间格式化器
    new SimpleDateFormat("HH:mm:ssss"), // 格式如13:00:1100
    new SimpleDateFormat("HH:mm:ss"), // 格式如13:00:11
    new SimpleDateFormat("HH:mm"), }; // 格式如13:00 @Override
    @SuppressWarnings("all")
    public Object convertValue(Map context, Object value, Class toType) { // 转换方法 if (toType.equals(java.sql.Date.class)) { // 如果是java.sql.Date 类型
    String[] parameterValues = (String[]) value;
    // 原始字符串数据
    for (DateFormat format : dateFormat)
    // 使用7种格式化器转换 日期
    try {
    return new java.sql.Date(format.parse(parameterValues[0])
    .getTime());
    } catch (ParseException e) {
    } } else if (toType.equals(java.sql.Time.class)) {
    // 如果是java.sql.Time类型
    String[] parameterValues = (String[]) value;
    // 原始字符串数据
    for (DateFormat format : timeFormat)
    // 使用3种格式化器转换时间
    try {
    return new java.sql.Time(format.parse(parameterValues[0])
    .getTime());
    } catch (ParseException e) {
    }
    } else if (toType.equals(java.util.Date.class)) {
    // 如果是java.util.Date类型
    String[] parameterValues = (String[]) value;
    // 原始字符串数据
    for (DateFormat format : dateFormat)
    // 使用7种格式化器转换日期
    try {
    return format.parse(parameterValues[0]);
    } catch (ParseException e) {
    }
    } else if (toType.equals(String.class)) { // 如果是字符串
    if (value instanceof java.sql.Date) {
    } else if (value instanceof java.sql.Time) {
    } else if (value instanceof java.util.Date) {
    return dateFormat[0].format((java.util.Date) value);
    // 将Date转为String
    }
    }
    return super.convertValue(context, value, toType);
    // 否则调用父类方法
    }
    }java.util.Date肯定要导入的,否则编译都无法通过。按网站上的参考:http://book.csdn.net/bookfiles/1209/100120936046.shtml
    升级了一下,不知情况怎么样。
      

  3.   

    现在的问题是刚重启是正常的,运行一两天后就显示不正常的日期:2010-01-01 00:00:00
    使用的是:struts2.1.18.1
      

  4.   

    应该是struts2的不稳定性造成的吧?
    或者说在哪个地方有对日期格式时行过修改?
      

  5.   

    <s:date name="createDate" format="yyyy-MM-dd"/>
      

  6.   

    <s:date name="date" format="yyyy-MM-dd HH:mm:ss"/>
      

  7.   

    通过加强版的DateConverter 经过一天的考验仍是正常的,但根本原因并未找出。仍需观察。