我们在POJO类中用到的时间类型为:
java.sql.Date;
java.sql.Time;
java.sql.Timestamp;
java.util.Date;
其中使用java.sql包中三个类型 的话,在pojo与vo对象转换的时候(通过BeanUtils.copyProperties(target, source);拷贝属性的),source类的日期时间field为null的话就会出错。而使用java.util.Date没有这个问题。
因为BeanUtils.copyProperties(target, source)只提供
java.sql.Date;
java.sql.Time;
java.sql.Timestamp
三个类型的转换。分别在org.apache.commons.beanutis.converters包下
SqlDateConverter;
SqlTimeConverter;
SqlTimestampConverter;
临时解决方法:
1.全部使用java.util.Date类型,
但在页面显示过程中,你本来输入"yyyy-MM-dd"的数据最后显示出来会是"yyyy-MM-dd HH:mm:ss"
其中修改src/service目录com.bright.util.DateUtil.getDateTimePattern()方法下,将返回结果不加时间类型,
既就返回return DateUtil.getDatePattern(); 这样在editForm中显示的日期类型就不加时间了.
其中在DisplayTag显示列中添加一个Decorator能解决display table中日期显示问题.如
*list.jsp文件中:
<display:column property="date1" sortable="true" headerClass="sortable"
titleKey="testtable3Form.date1"
decorator="com.bright.webapp.taglib.ShortDateDecorator" />
decorator类定义:
package com.bright.webapp.taglib;
import java.util.Date;
import org.apache.commons.lang.time.FastDateFormat;
import org.displaytag.decorator.ColumnDecorator;
public class ShortDateDecorator implements ColumnDecorator {
/**
* FastDateFormat used to format the date object.
*/
private FastDateFormat dateFormat = FastDateFormat
.getInstance("yyyy-MM-dd");
public final String decorate(Object columnValue) {
Date date = (Date) columnValue;
return this.dateFormat.format(date);
}
}
可参考:
2. 使用java.sql中的日期时间,通过
继承BeanUtils 写一个自己的对象拷贝field,如
其中SqlDateConverter()等处理类中考虑null拷贝不出错,可参考原来的SqlDateConverter()。
package com.bright.util;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.HashMap;
import java.util.Map;
import java.lang.reflect.*;
public final class BeanUtilEx
extends BeanUtils {
private static Map cache = new HashMap();
private static Log logger = LogFactory.getFactory().getInstance(BeanUtilEx.class);
private BeanUtilEx() {
}
static {
//注册sql.date的转换器,即允许BeanUtils.copyProperties时的源目标的sql类型的值允许为空
//ConvertUtils.register(new SqlDateConverter(), java.util.Date.class);
//ConvertUtils.register(new SqlTimestampConverter(), java.sql.Timestamp.class);
//注册util.date的转换器,即允许BeanUtils.copyProperties时的源目标的util类型的值允许为空
//ConvertUtils.register(new UtilDateConverter(), java.util.Date.class);
}
public static void copyProperties(Object target, Object source) throws
InvocationTargetException, IllegalAccessException {
//update bu zhuzf at 2004-9-29
//支持对日期copy
org.apache.commons.beanutils.BeanUtils.copyProperties(target, source);
}
}
待...
java.sql.Date;
java.sql.Time;
java.sql.Timestamp;
java.util.Date;
其中使用java.sql包中三个类型 的话,在pojo与vo对象转换的时候(通过BeanUtils.copyProperties(target, source);拷贝属性的),source类的日期时间field为null的话就会出错。而使用java.util.Date没有这个问题。
因为BeanUtils.copyProperties(target, source)只提供
java.sql.Date;
java.sql.Time;
java.sql.Timestamp
三个类型的转换。分别在org.apache.commons.beanutis.converters包下
SqlDateConverter;
SqlTimeConverter;
SqlTimestampConverter;
临时解决方法:
1.全部使用java.util.Date类型,
但在页面显示过程中,你本来输入"yyyy-MM-dd"的数据最后显示出来会是"yyyy-MM-dd HH:mm:ss"
其中修改src/service目录com.bright.util.DateUtil.getDateTimePattern()方法下,将返回结果不加时间类型,
既就返回return DateUtil.getDatePattern(); 这样在editForm中显示的日期类型就不加时间了.
其中在DisplayTag显示列中添加一个Decorator能解决display table中日期显示问题.如
*list.jsp文件中:
<display:column property="date1" sortable="true" headerClass="sortable"
titleKey="testtable3Form.date1"
decorator="com.bright.webapp.taglib.ShortDateDecorator" />
decorator类定义:
package com.bright.webapp.taglib;
import java.util.Date;
import org.apache.commons.lang.time.FastDateFormat;
import org.displaytag.decorator.ColumnDecorator;
public class ShortDateDecorator implements ColumnDecorator {
/**
* FastDateFormat used to format the date object.
*/
private FastDateFormat dateFormat = FastDateFormat
.getInstance("yyyy-MM-dd");
public final String decorate(Object columnValue) {
Date date = (Date) columnValue;
return this.dateFormat.format(date);
}
}
可参考:
2. 使用java.sql中的日期时间,通过
继承BeanUtils 写一个自己的对象拷贝field,如
其中SqlDateConverter()等处理类中考虑null拷贝不出错,可参考原来的SqlDateConverter()。
package com.bright.util;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.HashMap;
import java.util.Map;
import java.lang.reflect.*;
public final class BeanUtilEx
extends BeanUtils {
private static Map cache = new HashMap();
private static Log logger = LogFactory.getFactory().getInstance(BeanUtilEx.class);
private BeanUtilEx() {
}
static {
//注册sql.date的转换器,即允许BeanUtils.copyProperties时的源目标的sql类型的值允许为空
//ConvertUtils.register(new SqlDateConverter(), java.util.Date.class);
//ConvertUtils.register(new SqlTimestampConverter(), java.sql.Timestamp.class);
//注册util.date的转换器,即允许BeanUtils.copyProperties时的源目标的util类型的值允许为空
//ConvertUtils.register(new UtilDateConverter(), java.util.Date.class);
}
public static void copyProperties(Object target, Object source) throws
InvocationTargetException, IllegalAccessException {
//update bu zhuzf at 2004-9-29
//支持对日期copy
org.apache.commons.beanutils.BeanUtils.copyProperties(target, source);
}
}
待...