|
From: Eric P. <th...@us...> - 2010-12-15 00:55:30
|
Update of /cvsroot/sandev/sand/apps/basics/src/org/sandev/basics/util In directory sfp-cvsdas-3.v30.ch3.sourceforge.com:/tmp/cvs-serv13229 Modified Files: DateUtil.java Log Message: Added a utility method that knows how to read our standard USA datetime format. Just in case it sneaks into some bootstrap data that you want loaded in a different Locale. Index: DateUtil.java =================================================================== RCS file: /cvsroot/sandev/sand/apps/basics/src/org/sandev/basics/util/DateUtil.java,v retrieving revision 1.22 retrieving revision 1.23 diff -C2 -d -r1.22 -r1.23 *** DateUtil.java 17 Oct 2010 17:16:05 -0000 1.22 --- DateUtil.java 15 Dec 2010 00:55:22 -0000 1.23 *************** *** 474,477 **** --- 474,620 ---- /** + * Return true if the given string is understood as a standard + * full USA time specification, false otherwise. Occasionally you + * need to ship initial data created using one date format to + * other date format environments. The standard date query value + * format is probably the best general standard, but USA full + * shows up enough that it seems worth being able to just + * recognize and convert it if needed. This method tries to + * convert and if the conversion fails then it returns false. + */ + public static boolean isDefaultUSAFullValue(String val) + { + if(readDefaultUSAFullValue(val)!=null) { + return true; } + return false; + } + + + /** + * Some test dates that readDefaultUSAFullValue can handle + */ + public final static String[] USAFULLTESTDATES = { + "May 8, 2008 3:37:57 PM", + "Jan 1, 2010 12:00:00 AM", + "Jan 1, 2010 12:00:00 PM", + "Jan 1, 2010 3:00:00 PM", + "Jan 1, 2010 3:00 PM", + "Jan 1, 2010", + "Jan 1, 2010 15:00:00", + "Jan 1, 2010 15:00", + "Nov 25, 2008 10:15:53 AM" }; + + + /** + * Read the standard USA full date, regardless of what our current + * locale and date preferences are. Returns null if the date cannot + * be read. Example date: "Nov 25, 2008 10:15:53 AM" + */ + public static Date readDefaultUSAFullValue(String val) + { + if(!StringUtil.haveValue(val)) { + return null; } + val=val.toLowerCase().trim(); //just to be consistent + if(val.length()<11) { //nov 2, 2010 + return null; } + String monthstr=val.substring(0,3); //nov + //System.out.println("monthstr: " + monthstr); + int month=convertUSAMMM(monthstr); + //System.out.println("month: " + month); + if(month<0) { //Calendar.JANUARY==0 + return null; } + String rest=val.substring(4); //25, 2008 10:15:53 am + //System.out.println("rest: " + rest); + int commaindex=rest.indexOf(","); + if(commaindex<=0) { + return null; } + String daystr=rest.substring(0,commaindex); //25 + //System.out.println("daystr: " + daystr); + int day=StringUtil.safeGetIntValue(daystr); + //System.out.println("day: " + day); + if((day<=0)||(day>31)) { + return null; } + rest=rest.substring(commaindex+2); //2008 10:15:53 am + //System.out.println("rest: " + rest); + String yearstr=rest.substring(0,4); //2008 + //System.out.println("yearstr: " + yearstr); + int year=StringUtil.safeGetIntValue(yearstr); + //System.out.println("year: " + year); + if(year<=0) { + return null; } + rest=rest.substring(4); + rest=rest.trim(); //10:15:53 am + //System.out.println("rest: " + rest); + int hour=0; + int minutes=0; + int seconds=0; + if((rest.length()>=2)&&(Character.isDigit(rest.charAt(0)))) { + int hourstrlen=1; + if(Character.isDigit(rest.charAt(1))) { + hourstrlen=2; } + String hourstr=rest.substring(0,hourstrlen); + //System.out.println("hourstr: " + hourstr); + hour=StringUtil.safeGetIntValue(hourstr); + //System.out.println("hour: " + hour); + rest=rest.substring(hourstrlen); + if(rest.startsWith(":")) { + rest=rest.substring(1); } //15:53 am + //System.out.println("rest: " + rest); + if((rest.length()>=2)&&(Character.isDigit(rest.charAt(0)))&& + (Character.isDigit(rest.charAt(1)))) { + String minstr=rest.substring(0,2); + //System.out.println("minstr: " + minstr); + minutes=StringUtil.safeGetIntValue(minstr); + //System.out.println("minutes: " + minutes); + rest=rest.substring(2); + if(rest.startsWith(":")) { + rest=rest.substring(1); } } //53 am + //System.out.println("rest: " + rest); + if((rest.length()>=2)&&(Character.isDigit(rest.charAt(0)))&& + (Character.isDigit(rest.charAt(1)))) { + String secstr=rest.substring(0,2); + //System.out.println("secstr: " + secstr); + seconds=StringUtil.safeGetIntValue(secstr); + //System.out.println("seconds: " + seconds); + rest=rest.substring(2); + rest.trim(); } //am + //System.out.println("rest: " + rest); + if((rest.endsWith("am"))&&(hour==12)) { + hour=0; } + else if((rest.endsWith("pm"))&&(hour<12)) { + hour+=12; } } + //System.out.println("hour: " + hour); + Calendar cal=Calendar.getInstance(); + cal.set(year,month,day,hour,minutes,seconds); + return cal.getTime(); + } + + + /** + * Return the month as a calendar constant value, or -1 if the + * value is not understood. + */ + public static int convertUSAMMM(String month) + { + if(!StringUtil.haveValue(month)) { + return -1; } + month=month.toLowerCase().trim(); //may as well be nice about it. + if("jan".equals(month)) return Calendar.JANUARY; + if("feb".equals(month)) return Calendar.FEBRUARY; + if("mar".equals(month)) return Calendar.MARCH; + if("apr".equals(month)) return Calendar.APRIL; + if("may".equals(month)) return Calendar.MAY; + if("jun".equals(month)) return Calendar.JUNE; + if("jul".equals(month)) return Calendar.JULY; + if("aug".equals(month)) return Calendar.AUGUST; + if("sep".equals(month)) return Calendar.SEPTEMBER; + if("oct".equals(month)) return Calendar.OCTOBER; + if("nov".equals(month)) return Calendar.NOVEMBER; + if("dec".equals(month)) return Calendar.DECEMBER; + return -1; + } + + + /** * Return true if the given date is within the given number of * seconds from our current time, false otherwise. |