From: Eric P. <th...@us...> - 2010-09-28 23:50:57
|
Update of /cvsroot/sandev/sand/apps/basics/src/org/sandev/basics/util In directory sfp-cvsdas-3.v30.ch3.sourceforge.com:/tmp/cvs-serv27245 Modified Files: DateUtil.java StringUtil.java Log Message: Make the standard Date query format be understood by the StringUtil Date conversion utility. Index: DateUtil.java =================================================================== RCS file: /cvsroot/sandev/sand/apps/basics/src/org/sandev/basics/util/DateUtil.java,v retrieving revision 1.20 retrieving revision 1.21 diff -C2 -d -r1.20 -r1.21 *** DateUtil.java 25 Nov 2009 05:02:02 -0000 1.20 --- DateUtil.java 28 Sep 2010 23:50:49 -0000 1.21 *************** *** 324,328 **** * Return the query string format for this date value. This is the * quoted timestamp text format of the given date, which is used in ! * query processing. */ public static String getQueryValue(Calendar cal) --- 324,328 ---- * Return the query string format for this date value. This is the * quoted timestamp text format of the given date, which is used in ! * query processing. Format: YYYY-MM-DD HH:mm:ss.S */ public static String getQueryValue(Calendar cal) *************** *** 360,363 **** --- 360,365 ---- int millis=cal.get(Calendar.MILLISECOND); if(millis<10) { + buf.append("00"); } + else if(millis<100) { buf.append("0"); } buf.append(millis); *************** *** 367,370 **** --- 369,408 ---- /** + * Return true if the given string is understood as a standard + * date format query value, false otherwise. This allows for + * milliseconds to not be specified since it seems like that is + * probably the most useful thing to do. + */ + public static boolean isQueryValue(String val) + { + if(!StringUtil.haveValue(val)) { + return false; } + if(val.length()<19) { //must have at least seconds specified + return false; } + if((!Character.isDigit(val.charAt(0)))|| //Y + (!Character.isDigit(val.charAt(1)))|| //Y + (!Character.isDigit(val.charAt(2)))|| //Y + (!Character.isDigit(val.charAt(3)))|| //Y + (val.charAt(4)!='-')|| //- + (!Character.isDigit(val.charAt(5)))|| //M + (!Character.isDigit(val.charAt(6)))|| //M + (val.charAt(7)!='-')|| //- + (!Character.isDigit(val.charAt(8)))|| //D + (!Character.isDigit(val.charAt(9)))|| //D + (val.charAt(10)!=' ')|| // + (!Character.isDigit(val.charAt(11)))|| //H + (!Character.isDigit(val.charAt(12)))|| //H + (val.charAt(13)!=':')|| //: + (!Character.isDigit(val.charAt(14)))|| //m + (!Character.isDigit(val.charAt(15)))|| //m + (val.charAt(16)!=':')|| //: + (!Character.isDigit(val.charAt(17)))|| //s + (!Character.isDigit(val.charAt(18)))) { //s + return false; } + return true; + } + + + /** * Inverse operation for getQueryValue. Returns new Date(0) if * the given value could not be read. *************** *** 378,382 **** cal.set(Calendar.YEAR,StringUtil.safeGetIntValue(val.substring(0,4))); val=val.substring(5); //now after first '-' ! cal.set(Calendar.MONTH,StringUtil.safeGetIntValue(val.substring(0,2))); val=val.substring(3); //now after second '-' cal.set(Calendar.DAY_OF_MONTH, --- 416,423 ---- cal.set(Calendar.YEAR,StringUtil.safeGetIntValue(val.substring(0,4))); val=val.substring(5); //now after first '-' ! int monthEnum=StringUtil.safeGetIntValue(val.substring(0,2)); ! if(monthEnum > 0) { ! monthEnum--; } //in Java, Calendar.SEPTEMBER==8. WTF ! cal.set(Calendar.MONTH,monthEnum); val=val.substring(3); //now after second '-' cal.set(Calendar.DAY_OF_MONTH, *************** *** 391,396 **** cal.set(Calendar.SECOND, StringUtil.safeGetIntValue(val.substring(0,2))); ! val=val.substring(3); //now after '.' ! cal.set(Calendar.MILLISECOND,StringUtil.safeGetIntValue(val)); return cal.getTime(); } --- 432,440 ---- cal.set(Calendar.SECOND, StringUtil.safeGetIntValue(val.substring(0,2))); ! if(val.length()<3) { ! cal.set(Calendar.MILLISECOND,0); } ! else { ! val=val.substring(3); //now after '.' ! cal.set(Calendar.MILLISECOND,StringUtil.safeGetIntValue(val)); } return cal.getTime(); } Index: StringUtil.java =================================================================== RCS file: /cvsroot/sandev/sand/apps/basics/src/org/sandev/basics/util/StringUtil.java,v retrieving revision 1.40 retrieving revision 1.41 diff -C2 -d -r1.40 -r1.41 *** StringUtil.java 8 Mar 2010 23:11:33 -0000 1.40 --- StringUtil.java 28 Sep 2010 23:50:49 -0000 1.41 *************** *** 456,461 **** (convertsToLong(val))) { //debugout("getDateValue reading date from long " + val); ! long time=getLongValue(val.trim()); return new Date(time); } return df.parse(val.trim()); } catch(ParseException e) { --- 456,465 ---- (convertsToLong(val))) { //debugout("getDateValue reading date from long " + val); ! long time=getLongValue(val); return new Date(time); } + //if standard date query value format, then convert it + else if(DateUtil.isQueryValue(val)) { + return DateUtil.readQueryValue(val); } + //otherwise try and parse it return df.parse(val.trim()); } catch(ParseException e) { |