对struts2日期处理的思考(一)

来源:互联网 发布:诺基亚windows手机 编辑:程序博客网 时间:2024/06/05 09:21

今天遇到一个问题,就是前端页面提交一个字符串的日期,struts2的DateConverter处理能力是有有限的,并不能达到理想的转换效果。

所以,自己上网搜集了一下。


struts2日期处理源代码如下:

关于DateFormat的一下说明



DateConverter的处理逻辑是


1、如果是Date类型,则直接强转

 if (Date.class.isAssignableFrom(value.getClass())) {      result = (Date) value; }

2、如果是字符串,分为三类处理

a、if (java.sql.Time.class == toType)

sql的time使用这个格式去解析: df = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);

b、if (java.sql.Timestamp.class == toType)

sql的time的timestamp则以三种格式的的df来解析,类型和顺序如下

 

Date check = null;                SimpleDateFormat dtfmt = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT,                        DateFormat.MEDIUM,                        locale);//格式2                SimpleDateFormat fullfmt = new SimpleDateFormat(dtfmt.toPattern() + MILLISECOND_FORMAT,                        locale);//格式1                SimpleDateFormat dfmt = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT,                        locale);//格式3                SimpleDateFormat[] fmts = {fullfmt, dtfmt, dfmt};//三种格式的顺序                for (SimpleDateFormat fmt : fmts) {                    try {                        check = fmt.parse(sa);                        df = fmt;                        if (check != null) {                            break;                        }                    } catch (ParseException ignore) {                    }                }


c、if (java.util.Date.class == toType)

util的Date,则试图以七种格式去解析,代码如下:

1、

Date check;                DateFormat[] dfs = getDateFormats(locale);                for (DateFormat df1 : dfs) {                    try {                        check = df1.parse(sa);                        df = df1;                        if (check != null) {                            break;                        }                    } catch (ParseException ignore) {                    }                }
2、

private DateFormat[] getDateFormats(Locale locale) {        DateFormat dt1 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);        DateFormat dt2 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);        DateFormat dt3 = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);        DateFormat d1 = DateFormat.getDateInstance(DateFormat.SHORT, locale);        DateFormat d2 = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);        DateFormat d3 = DateFormat.getDateInstance(DateFormat.LONG, locale);        DateFormat rfc3399 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");        return new DateFormat[]{dt1, dt2, dt3, rfc3399, d1, d2, d3};//七种格式的顺序    }

3、通过以上的处理后,最后,进行实际的解析:

//final fallback for dates without time            if (df == null) {                df = DateFormat.getDateInstance(DateFormat.SHORT, locale);            }            try {                df.setLenient(false); // let's use strict parsing (XW-341)                result = df.parse(sa);                if (!(Date.class == toType)) {                    try {                        Constructor constructor = toType.getConstructor(new Class[]{long.class});                        return constructor.newInstance(new Object[]{Long.valueOf(result.getTime())});                    } catch (Exception e) {                        throw new XWorkException("Couldn't create class " + toType + " using default (long) constructor", e);                    }                }            } catch (ParseException e) {                throw new XWorkException("Could not parse date", e);            }


struts2经过1~3几个步骤,完成日期的解析。但是,我们发现,它能够处理的格式是有限的。所以,需要我们的扩展。


具体的解决方案:


前期基础扩知识,请参看:


Struts2开发 基本配置与类型转换:

http://www.jb51-(confusion).net/article/35468.htm


struts2 类型转换器 Date型数据转换 :

http://blog.csdn.net/csophys/article/details/6866776


struts2自定义DateConverter的陷阱:(多线程并发)

http://my.oschina-(confusion).net/hotleave/blog/133948


struts2的时间格式转换问题:

http://jolestar.iteye.com/blog/174444


PS:csdn的blog中不让出现的url我的会加上“--(confusion)”,实在是无奈之举。CSDN这样有意思吗???


0 0
原创粉丝点击