struts2 格式化输出问题 在tomcat6上面本来日期时间可以正常显示,但运行一段时间后,所有的<s:date 输出的内容变为: 2010-01-01 00:00:00,不知是什么原因啊。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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 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升级了一下,不知情况怎么样。 现在的问题是刚重启是正常的,运行一两天后就显示不正常的日期:2010-01-01 00:00:00使用的是:struts2.1.18.1 应该是struts2的不稳定性造成的吧?或者说在哪个地方有对日期格式时行过修改? <s:date name="createDate" format="yyyy-MM-dd"/> <s:date name="date" format="yyyy-MM-dd HH:mm:ss"/> 通过加强版的DateConverter 经过一天的考验仍是正常的,但根本原因并未找出。仍需观察。 用jquery,发现的问题,求助各位大大 如何使用resteasy获取post过来的body? Linux下如何安装Oracle (安装成功过的进) java查询oracle数据库问题! 熟悉JasperReport的请进 请教List方面的问题 一个关于sql连接的小问题(在线等待) jsp调用javabean问题 HTML如何能播放.m3u8视频文件 本公司诚聘java,jsp程序员,有意者请与我们联系 有个问题向用过google map的高手们请教 jsp自制标签
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
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
升级了一下,不知情况怎么样。
使用的是:struts2.1.18.1
或者说在哪个地方有对日期格式时行过修改?