private Object doConvertToDate(Map context, Object value, Class toType) ...{ Date result =null; if (value instanceof String && value !=null&& ((String)value).length() >0) ...{ String sa = (String) value; Locale locale = getLocale(context); DateFormat df =null; if (java.sql.Time.class== toType) ...{ df = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale); }elseif (java.sql.Timestamp.class== toType) ...{ Date check =null; SimpleDateFormat dtfmt = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale); SimpleDateFormat fullfmt =new SimpleDateFormat(dtfmt.toPattern() + MILLISECOND_FORMAT, locale); SimpleDateFormat dfmt = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT, locale); SimpleDateFormat[] fmts =...{fullfmt, dtfmt, dfmt}; for (int i =0; i < fmts.length; i++) ...{ try...{ check = fmts[i].parse(sa); df = fmts[i]; if (check !=null) ...{ break; } }catch (ParseException ignore) ...{ } } }elseif(java.util.Date.class== toType) ...{ Date check =null; SimpleDateFormat d1 = (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale); SimpleDateFormat d2 = (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale); SimpleDateFormat d3 = (SimpleDateFormat)DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale); SimpleDateFormat[] dfs =...{d1, d2, d3, RFC3399_FORMAT}; //added RFC 3339 date format (XW-473) for (int i =0; i < dfs.length; i++) ...{ try...{ check = dfs[i].parse(sa); df = dfs[i]; if (check !=null) ...{ break; } } catch (ParseException ignore) ...{ } } } //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[]...{new Long(result.getTime())}); }catch (Exception e) ...{ thrownew XWorkException("Couldn't create class "+ toType +" using default (long) constructor", e); } } }catch (ParseException e) ...{ thrownew XWorkException("Could not parse date", e); } }elseif (Date.class.isAssignableFrom(value.getClass())) ...{ result = (Date) value; } return result; }
完成转换的函数:
public Object convertValue(Map context, Object o, Member member, String s, Object value, Class toType) ...{ Object result =null; if (value ==null|| toType.isAssignableFrom(value.getClass())) ...{ // no need to convert at all, right? return value; } if (toType == String.class) ...{ result = doConvertToString(context, value); }elseif (toType ==boolean.class) ...{ result = doConvertToBoolean(value); &n