|
From: todd r. <to...@us...> - 2005-03-31 20:58:02
|
Update of /cvsroot/pocolap/pocolap/data/scripts_tools/outdated/oldtools/com/pocolap/hsqldb/datetime In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9992/data/scripts_tools/outdated/oldtools/com/pocolap/hsqldb/datetime Added Files: DateAgent.java Log Message: Reorganization of resources --- NEW FILE: DateAgent.java --- /* * DateAgent.java * Created on Jan 23, 2004 * Copyright (c) 2003 Todd Runstein Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.pocolap.hsqldb.datetime; import java.sql.Date; import java.text.ParseException; import java.text.SimpleDateFormat; /** * DateAgent is intended to be used when building an HSQLDB database * using the script created by the tools provided with pocolap. * * This uses deprecated methods - if you have a problem with that, go ahead * and write it! * * @author todd * */ public class DateAgent { /* ARGs match MySQL arguments for "DATE_FORMAT" method */ private static String ARG_DAY_NAME = "%W"; private static String ARG_DAY_ABRV = "%a"; private static String ARG_DAY_NUMBER = "%w"; private static String ARG_DAY_OF_MONTH_NUMBER = "%e"; private static String ARG_MONTH_NAME = "%M"; private static String ARG_MONTH_ABRV = "%b"; private static String ARG_MONTH_NUMBER = "%c"; private static String ARG_YEAR_NUMBER = "%Y"; private static String[] FULL_DAYS = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; private static String[] FULL_MONTHS = {"January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"}; public static Date dateAdd(Date date, long increment){ return new Date(date.getTime()+(1000*60*60*24*increment)); } public static String dateFormat(Date date,String arg){ String retVal = null; if (arg.equals(ARG_DAY_NAME)){ retVal = doFullDayName(date); }else if(arg.equals(ARG_DAY_ABRV)){ retVal = doAbbrvDayName(date); }else if(arg.equals(ARG_DAY_NUMBER)){ retVal = new Integer(doDayNumber(date)).toString(); }else if(arg.equals(ARG_DAY_OF_MONTH_NUMBER)){ retVal = new Integer(doDayOfMonthNumber(date)).toString(); }else if(arg.equals(ARG_MONTH_NAME)){ retVal = doFullMonthName(date); }else if (arg.equals(ARG_MONTH_ABRV)){ retVal = doAbbrvMonthName(date); }else if(arg.equals(ARG_MONTH_NUMBER)){ retVal = new Integer(doMonthNumber(date)).toString(); }else if(arg.equals(ARG_YEAR_NUMBER)){ retVal = new Integer(doYearNumber(date)).toString(); } return retVal; } private static int getMonthsInt(Date date){ return date.getMonth(); } protected static int doYearNumber(Date date){ return date.getYear()+1900; } protected static int doMonthNumber(Date date){ return getMonthsInt(date)+1; } protected static int doDayNumber(Date date){ return date.getDay(); } protected static int doDayOfMonthNumber(Date date){ return date.getDate(); } protected static String doFullMonthName(Date date){ return FULL_MONTHS[getMonthsInt(date)]; } protected static String doAbbrvMonthName(Date date){ String retVal = null; String fullMonth = doFullMonthName(date); if (fullMonth!=null && fullMonth.length()>2){ retVal = fullMonth.substring(0,3); } return retVal; } protected static String doFullDayName(Date date){ return FULL_DAYS[doDayNumber(date)]; } protected static String doAbbrvDayName(Date date){ String retVal = null; String fullDay = doFullDayName(date); if (fullDay!=null && fullDay.length()>3){ retVal = fullDay.substring(0,3); } return retVal; } public static void main(String[] args){ //In lieu of a formal JUnit test! if (args.length<1){ System.out.println("USAGE: (<\"true\">)|(<date><increment>) "); return; } if (args.length==1){ if (args[0].equalsIgnoreCase("true")){ doAutoTest(); }else{ System.out.println("USAGE: (<\"true\">)|(<date><increment>) "); return; } }else{ doUserTest(args); } } public static void doAutoTest(){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { Date One = new Date(format.parse("2003-12-29").getTime()); Date One2 = new Date(format.parse("2003-12-31").getTime()); Date One7 = new Date(format.parse("2004-1-5").getTime()); Date One9 = new Date(format.parse("2004-1-7").getTime()); Date mOne2 = new Date(format.parse("2003-12-27").getTime()); Date mOne7 = new Date(format.parse("2003-12-22").getTime()); Date mOne9 = new Date(format.parse("2003-12-20").getTime()); Date tMay = new Date(format.parse("2003-05-15").getTime()); int i2 = 2; int i7 = 7; int i9 = 9; Date tOne = DateAgent.dateAdd(One, i2); String testVal = null; if (!tOne.equals(One2)){ //Check date System.out.println("Failure: "+tOne.toString() + " should be " + One2.toString()); }else{ //Check days testVal = dateFormat(tOne,"%W"); if (!testVal.equals(FULL_DAYS[3])){ System.out.println("Failure: "+tOne.toString() + " reporting as " + testVal + " should be " + FULL_DAYS[3]); } testVal = dateFormat(tOne,"%a"); if (!testVal.equals("Wed")){ System.out.println("Failure:" + tOne.toString() + " reporting as \"" + testVal + "\" should be \"Wed\"" ); } testVal = dateFormat(tOne,"%w"); if (!testVal.equals("3")){ System.out.println("Failure:" + tOne.toString() + " reporting as \"" + testVal + "\" should be \"3\"" ); } testVal = dateFormat(tOne,"%e"); if (!testVal.equals("31")){ System.out.println("Failure:" + tOne.toString() + " reporting as \"" + testVal + "\" should be \"31\"" ); } testVal = dateFormat(tOne,"%M"); if (!testVal.equals("December")){ System.out.println("Failure:" + tOne.toString() + " reporting as \"" + testVal + "\" should be \"December\"" ); } testVal = dateFormat(tOne,"%M"); if (!testVal.equals("December")){ System.out.println("Failure:" + tOne.toString() + " reporting as \"" + testVal + "\" should be \"December\"" ); } testVal = dateFormat(tOne,"%b"); if (!testVal.equals("Dec")){ System.out.println("Failure:" + tOne.toString() + " reporting as \"" + testVal + "\" should be \"Dec\"" ); } //Special test for May testVal = dateFormat(tMay,"%b"); if (!testVal.equals("May")){ System.out.println("Failure:" + tOne.toString() + " reporting as \"" + testVal + "\" should be \"May\"" ); } testVal = dateFormat(tOne,"%Y"); if (!testVal.equals("2003")){ System.out.println("Failure:" + tOne.toString() + " reporting as \"" + testVal + "\" should be \"2003\"" ); } } //Here's the deal: you want more test, write them! tOne = DateAgent.dateAdd(One, (i2*-1)); if (!tOne.equals(mOne2)){ System.out.println("Failure: "+tOne.toString() + " should be " + mOne2.toString()); } tOne = DateAgent.dateAdd(One, i7); if (!tOne.equals(One7)){ System.out.println("Failure: "+tOne.toString() + " should be " + One7.toString()); } tOne = DateAgent.dateAdd(One, (i7*-1)); if (!tOne.equals(mOne7)){ System.out.println("Failure: "+tOne.toString() + " should be " + mOne7.toString()); } tOne = DateAgent.dateAdd(One, i9); if (!tOne.equals(One9)){ System.out.println("Failure: "+tOne.toString() + " should be " + One9.toString()); } tOne = DateAgent.dateAdd(One, (i9*-1)); if (!tOne.equals(mOne9)){ System.out.println("Failure: "+tOne.toString() + " should be " + mOne9.toString()); } } catch (ParseException e) { System.out.println("The dates used for testing can't be parsed - "+ "it's nothing you did, but you'll need to look at the doAutoTest method " + "to see what's wrong. Sorry!"); } System.out.println("Test complete"); } public static void doUserTest(String[] args){ Date date = null; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { java.util.Date tDate = format.parse(args[0]); date = new Date(tDate.getTime()); } catch (ParseException e) { System.out.println(args[0] + " is not in the right format (\"yyyy-MM-dd\")"); return; } long increment = 0; try{ increment = new Integer(args[1]).intValue(); }catch(NumberFormatException e){ System.out.println(args[1] + " can't be cast to an integer. Try again"); return; } String fullday = doFullDayName(date); System.out.println("Starting with "+date.toString() + " which is a " + fullday); Date newDate = DateAgent.dateAdd(date,increment); fullday = doFullDayName(newDate); System.out.println("Value returned: "+newDate.toString() + " which is a "+fullday); System.out.println("Done"); } } |