Struts2 java.sql.Timestamp到日历类型转换

Struts2 java.sql.Timestamp到日历类型转换,struts2,timestamp,typeconverter,Struts2,Timestamp,Typeconverter,在Struts 2 Action类中,有List对象。每个对象数组的一个元素是java.sql.Timestamp,我使用Struts标记显示它。我是否可以使用应用程序级转换将数据从时间戳转换为UTC日历?我尝试使用自定义转换器,并将以下行添加到xwork conversion.properties: java.sql.Timestamp=com.test.CalendarConverter 但是,这并不影响输出,甚至没有调用CalendarConverter的convertToString方

在Struts 2 Action类中,有
List
对象。每个对象数组的一个元素是
java.sql.Timestamp
,我使用
Struts标记显示它。我是否可以使用应用程序级转换将数据从
时间戳
转换为UTC日历?我尝试使用自定义转换器,并将以下行添加到
xwork conversion.properties

java.sql.Timestamp=com.test.CalendarConverter
但是,这并不影响输出,甚至没有调用
CalendarConverter
convertToString
方法。下面是
日历转换器

public class CalendarConverter extends StrutsTypeConverter {

Log logger = LogFactory.getLog(CalendarConverter.class);
String format = null;

public CalendarConverter() {
    format = "MM/dd/yyyy HH:mm";
}

public Object convertFromString(Map context, String [] values, Class toClass) {
    Calendar cal = null;
    logger.debug("-----------------> In convertFromString: value[0]- " + ((values == null || values.length < 1) ? "NULL" : values[0]));
    if (values != null && values.length == 1) {
        if (values[0].trim().length() > 0) {
            try {
                TimeZone tz = TimeZone.getDefault();
                tz = userSession.getTimeZone();
                if (tz == null) {
                    tz = TimeZone.getDefault();
                }
                DateFormat dateFormat = null; 
                if (isValidDate(values[0], format)) {
                    dateFormat = new SimpleDateFormat(format);
                }
                dateFormat.setLenient(false);
                dateFormat.setTimeZone(tz);
                Date dt = dateFormat.parse(values[0]);
                cal = Calendar.getInstance(tz);
                cal.setTime(dt);
            } catch (Exception ex) {
                logger.error("Error in date conversion");
                logger.debug(ex.getMessage());
                //throw new TypeConversionException(ex);
                }
            }
        }

    return cal;
}

public boolean isValidDate(String str, String format) {
    DateFormat df = new SimpleDateFormat(format);
    df.setLenient(false);
    ParsePosition pos = new ParsePosition(0);
    df.parse(str, pos);
    if (pos.getErrorIndex() == -1 && pos.getIndex() == str.length() )
        return true;
    else
        return false;
}

public String convertToString(Map context, Object o) {
    String data = null;
    DateFormat dateFormat = new SimpleDateFormat(format);
    TimeZone tz = TimeZone.getDefault(); 
    tz = TimeZone.getTimeZone("UTC");
    if (tz == null) {
        tz = TimeZone.getDefault();
    }
    dateFormat.setTimeZone(tz);
    logger.debug("-----------------> In convertToString: o- " + o + " (" + (o == null ? null : o.getClass().getName()) + ")");

    if (o != null) {
        if (o instanceof Calendar) {
            Calendar cal = (Calendar)o;
            data = dateFormat.format(cal.getTime());
        } else if (o instanceof Date) {
            data = dateFormat.format((Date)o);
        } else if (o instanceof Timestamp) {
            data = dateFormat.format((Timestamp)o);
        }
    }

    return data;
}
}
公共类CalendarConverter扩展了StrutsTypeConverter{
日志记录器=LogFactory.getLog(CalendarConverter.class);
字符串格式=空;
公共日历转换器(){
format=“MM/dd/yyyy HH:MM”;
}
公共对象convertFromString(映射上下文,字符串[]值,类到类){
日历cal=空;
logger.debug(“------------------>在convertFromString:value[0]-”+((values==null | | | values.length<1)?“null”:values[0]);
if(值!=null&&values.length==1){
如果(值[0].trim().length()>0){
试一试{
时区tz=TimeZone.getDefault();
tz=userSession.getTimeZone();
如果(tz==null){
tz=时区。getDefault();
}
DateFormat DateFormat=null;
if(isValidDate(值[0],格式)){
dateFormat=新的SimpleDataFormat(格式);
}
dateFormat.setLenient(false);
dateFormat.setTimeZone(tz);
Date dt=dateFormat.parse(值[0]);
cal=Calendar.getInstance(tz);
校准设定时间(dt);
}捕获(例外情况除外){
记录器错误(“日期转换错误”);
debug(例如getMessage());
//抛出新的TypeConversionException(ex);
}
}
}
返回cal;
}
公共布尔值isValidDate(字符串str,字符串格式){
DateFormat df=新的SimpleDataFormat(格式);
df.setLenient(假);
ParsePosition pos=新的ParsePosition(0);
解析(str,pos);
if(pos.getErrorIndex()=-1&&pos.getIndex()==str.length())
返回true;
其他的
返回false;
}
公共字符串convertToString(映射上下文,对象o){
字符串数据=null;
DateFormat DateFormat=新的SimpleDateFormat(格式);
时区tz=TimeZone.getDefault();
tz=时区。getTimeZone(“UTC”);
如果(tz==null){
tz=时区。getDefault();
}
dateFormat.setTimeZone(tz);
logger.debug(“------------------>在convertToString:o-“+o+”(“+(o==null?null:o.getClass().getName())+”);
如果(o!=null){
if(日历的o实例){
日历cal=(日历)o;
data=dateFormat.format(cal.getTime());
}else if(o instanceof Date){
数据=日期格式。格式((日期)o);
}else if(o instanceof Timestamp){
data=dateFormat.format((时间戳)o);
}
}
返回数据;
}
}

您将您的
xwork转换.properties
放在哪里了?正在调用您的
CalendarConverter
构造函数吗?是否有错误?你怎么打电话给你的转换器的?你为什么认为应该叫它?您知道什么是运行时异常吗?@AleksandrM xwork-conversion.properties在类级别(web inf\classes)。当基础对象是java.util.Calendar时,CalendarConverter被正确调用。@RomanC:我没有收到任何错误或异常。当对象的类型为java.sql.Timestamp时,似乎没有调用convertToString函数。如果对象的类型为java.util.CalendarPost,那么它可以正常工作。