You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/06/24 01:41:10 UTC

cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang/time DateUtils.java

scolebourne    2003/06/23 16:41:10

  Modified:    lang/src/test/org/apache/commons/lang/time
                        DateUtilsTest.java
               lang/src/java/org/apache/commons/lang/time DateUtils.java
  Log:
  Prepare DateUtils for 2.0 release
  
  Revision  Changes    Path
  1.4       +122 -58   jakarta-commons/lang/src/test/org/apache/commons/lang/time/DateUtilsTest.java
  
  Index: DateUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/time/DateUtilsTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DateUtilsTest.java	8 Jun 2003 23:14:23 -0000	1.3
  +++ DateUtilsTest.java	23 Jun 2003 23:41:10 -0000	1.4
  @@ -153,62 +153,104 @@
           assertEquals("round second-2 failed",
                   dateTimeParser.parse("November 18, 2001 1:23:11.000"),
                   DateUtils.round(date2, Calendar.SECOND));
  +                
  +        try {
  +            DateUtils.round((Date) null, Calendar.SECOND);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            DateUtils.round((Calendar) null, Calendar.SECOND);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            DateUtils.round((Object) null, Calendar.SECOND);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            DateUtils.round("", Calendar.SECOND);
  +            fail();
  +        } catch (ClassCastException ex) {}
       }
   
       /**
        * Tests various values with the trunc method
        */
  -    public void testTrunc() throws Exception {
  -        assertEquals("trunc year-1 failed",
  +    public void testTruncate() throws Exception {
  +        assertEquals("truncate year-1 failed",
                   dateParser.parse("January 1, 2002"),
  -                DateUtils.trunc(date1, Calendar.YEAR));
  -        assertEquals("trunc year-2 failed",
  +                DateUtils.truncate(date1, Calendar.YEAR));
  +        assertEquals("truncate year-2 failed",
                   dateParser.parse("January 1, 2001"),
  -                DateUtils.trunc(date2, Calendar.YEAR));
  -        assertEquals("trunc month-1 failed",
  +                DateUtils.truncate(date2, Calendar.YEAR));
  +        assertEquals("truncate month-1 failed",
                   dateParser.parse("February 1, 2002"),
  -                DateUtils.trunc(date1, Calendar.MONTH));
  -        assertEquals("trunc month-2 failed",
  +                DateUtils.truncate(date1, Calendar.MONTH));
  +        assertEquals("truncate month-2 failed",
                   dateParser.parse("November 1, 2001"),
  -                DateUtils.trunc(date2, Calendar.MONTH));
  -        assertEquals("trunc semimonth-1 failed",
  +                DateUtils.truncate(date2, Calendar.MONTH));
  +        assertEquals("truncate semimonth-1 failed",
                   dateParser.parse("February 1, 2002"),
  -                DateUtils.trunc(date1, DateUtils.SEMI_MONTH));
  -        assertEquals("trunc semimonth-2 failed",
  +                DateUtils.truncate(date1, DateUtils.SEMI_MONTH));
  +        assertEquals("truncate semimonth-2 failed",
                   dateParser.parse("November 16, 2001"),
  -                DateUtils.trunc(date2, DateUtils.SEMI_MONTH));
  -        assertEquals("trunc date-1 failed",
  +                DateUtils.truncate(date2, DateUtils.SEMI_MONTH));
  +        assertEquals("truncate date-1 failed",
                   dateParser.parse("February 12, 2002"),
  -                DateUtils.trunc(date1, Calendar.DATE));
  -        assertEquals("trunc date-2 failed",
  +                DateUtils.truncate(date1, Calendar.DATE));
  +        assertEquals("truncate date-2 failed",
                   dateParser.parse("November 18, 2001"),
  -                DateUtils.trunc(date2, Calendar.DATE));
  -        assertEquals("trunc hour-1 failed",
  +                DateUtils.truncate(date2, Calendar.DATE));
  +        assertEquals("truncate hour-1 failed",
                   dateTimeParser.parse("February 12, 2002 12:00:00.000"),
  -                DateUtils.trunc(date1, Calendar.HOUR));
  -        assertEquals("trunc hour-2 failed",
  +                DateUtils.truncate(date1, Calendar.HOUR));
  +        assertEquals("truncate hour-2 failed",
                   dateTimeParser.parse("November 18, 2001 1:00:00.000"),
  -                DateUtils.trunc(date2, Calendar.HOUR));
  -        assertEquals("trunc minute-1 failed",
  +                DateUtils.truncate(date2, Calendar.HOUR));
  +        assertEquals("truncate minute-1 failed",
                   dateTimeParser.parse("February 12, 2002 12:34:00.000"),
  -                DateUtils.trunc(date1, Calendar.MINUTE));
  -        assertEquals("trunc minute-2 failed",
  +                DateUtils.truncate(date1, Calendar.MINUTE));
  +        assertEquals("truncate minute-2 failed",
                   dateTimeParser.parse("November 18, 2001 1:23:00.000"),
  -                DateUtils.trunc(date2, Calendar.MINUTE));
  -        assertEquals("trunc second-1 failed",
  +                DateUtils.truncate(date2, Calendar.MINUTE));
  +        assertEquals("truncate second-1 failed",
                   dateTimeParser.parse("February 12, 2002 12:34:56.000"),
  -                DateUtils.trunc(date1, Calendar.SECOND));
  -        assertEquals("trunc second-2 failed",
  +                DateUtils.truncate(date1, Calendar.SECOND));
  +        assertEquals("truncate second-2 failed",
                   dateTimeParser.parse("November 18, 2001 1:23:11.000"),
  -                DateUtils.trunc(date2, Calendar.SECOND));
  +                DateUtils.truncate(date2, Calendar.SECOND));
   
  +        try {
  +            DateUtils.truncate((Date) null, Calendar.SECOND);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            DateUtils.truncate((Calendar) null, Calendar.SECOND);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            DateUtils.truncate((Object) null, Calendar.SECOND);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            DateUtils.truncate("", Calendar.SECOND);
  +            fail();
  +        } catch (ClassCastException ex) {}
       }
   
       /**
        * Tests the parse method, which is supposed to handle various strings
        * as flexibly as CVS supports.
        */
  -    public void testParse() throws Exception {
  +    public void testParseCVS() throws Exception {
  +        try {
  +            DateUtils.parseCVS(null);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            DateUtils.parseCVS("gobbledegook");
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        
           //This is difficult to test since the "now" used in the
           //  parse function cannot be controlled.  We could possibly control
           //  it by trying before and after and making sure the value we expect
  @@ -219,22 +261,22 @@
   
           now = Calendar.getInstance();
           now.add(Calendar.MINUTE, -1);
  -        assertEquals("parse 1 minute ago",
  -                now, DateUtils.parse("1 minute ago"), 50);
  +        assertEquals("parseCVS 1 minute ago",
  +                now, DateUtils.parseCVS("1 minute ago"), 50);
           now = Calendar.getInstance();
           now.add(Calendar.MINUTE, -8);
  -        assertEquals("parse 8 minutes ago",
  -                now, DateUtils.parse("8 minutes ago"), 50);
  +        assertEquals("parseCVS 8 minutes ago",
  +                now, DateUtils.parseCVS("8 minutes ago"), 50);
   
           now = Calendar.getInstance();
           now.add(Calendar.DATE, -1);
  -        assertEquals("parse yesterday",
  -                now, DateUtils.parse("yesterday"), 50);
  +        assertEquals("parseCVS yesterday",
  +                now, DateUtils.parseCVS("yesterday"), 50);
   
           now = Calendar.getInstance();
           now.add(Calendar.DATE, 1);
  -        assertEquals("parse tomorrow",
  -                now, DateUtils.parse("tomorrow"), 50);
  +        assertEquals("parseCVS tomorrow",
  +                now, DateUtils.parseCVS("tomorrow"), 50);
   
           now = Calendar.getInstance();
           //Sunday would be 1, Saturday would be 7, so we walk back up to 6 days.
  @@ -244,13 +286,13 @@
           } else {
               now.add(Calendar.DATE, 1 - now.get(Calendar.DAY_OF_WEEK));
           }
  -        assertEquals("parse last Sunday",
  -                now, DateUtils.parse("last Sunday"), 50);
  +        assertEquals("parseCVS last Sunday",
  +                now, DateUtils.parseCVS("last Sunday"), 50);
   
           now = Calendar.getInstance();
           now.add(Calendar.DATE, -7);
  -        assertEquals("parse last week",
  -                now, DateUtils.parse("last week"), 50);
  +        assertEquals("parseCVS last week",
  +                now, DateUtils.parseCVS("last week"), 50);
   
           now = Calendar.getInstance();
           //January would be 0, December would be 11, so we walk back up to 11 months
  @@ -260,8 +302,30 @@
           } else {
               now.add(Calendar.MONTH, 0 - now.get(Calendar.MONTH));
           }
  -        assertEquals("parse last January",
  -                now, DateUtils.parse("last January"), 50);
  +        assertEquals("parseCVS last January",
  +                now, DateUtils.parseCVS("last January"), 50);
  +    }
  +
  +    /**
  +     * Tests the iterator exceptions
  +     */
  +    public void testIteratorEx() throws Exception {
  +        try {
  +            DateUtils.iterator((Date) null, DateUtils.RANGE_WEEK_CENTER);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            DateUtils.iterator((Calendar) null, DateUtils.RANGE_WEEK_CENTER);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            DateUtils.iterator((Object) null, DateUtils.RANGE_WEEK_CENTER);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            DateUtils.iterator("", DateUtils.RANGE_WEEK_CENTER);
  +            fail();
  +        } catch (ClassCastException ex) {}
       }
   
       /**
  @@ -270,51 +334,51 @@
       public void testWeekIterator() throws Exception {
           Calendar now = Calendar.getInstance();
           for (int i = 0; i< 7; i++) {
  -            Calendar today = DateUtils.trunc(now, Calendar.DATE);
  -            Calendar sunday = DateUtils.trunc(now, Calendar.DATE);
  +            Calendar today = DateUtils.truncate(now, Calendar.DATE);
  +            Calendar sunday = DateUtils.truncate(now, Calendar.DATE);
               sunday.add(Calendar.DATE, 1 - sunday.get(Calendar.DAY_OF_WEEK));
  -            Calendar monday = DateUtils.trunc(now, Calendar.DATE);
  +            Calendar monday = DateUtils.truncate(now, Calendar.DATE);
               if (monday.get(Calendar.DAY_OF_WEEK) == 1) {
                   //This is sunday... roll back 6 days
                   monday.add(Calendar.DATE, -6);
               } else {
                   monday.add(Calendar.DATE, 2 - monday.get(Calendar.DAY_OF_WEEK));
               }
  -            Calendar centered = DateUtils.trunc(now, Calendar.DATE);
  +            Calendar centered = DateUtils.truncate(now, Calendar.DATE);
               centered.add(Calendar.DATE, -3);
               
  -            Iterator it = DateUtils.getCalendarIterator(now, DateUtils.RANGE_WEEK_SUNDAY);
  +            Iterator it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_SUNDAY);
               assertWeekIterator(it, sunday);
  -            it = DateUtils.getCalendarIterator(now, DateUtils.RANGE_WEEK_MONDAY);
  +            it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_MONDAY);
               assertWeekIterator(it, monday);
  -            it = DateUtils.getCalendarIterator(now, DateUtils.RANGE_WEEK_RELATIVE);
  +            it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_RELATIVE);
               assertWeekIterator(it, today);
  -            it = DateUtils.getCalendarIterator(now, DateUtils.RANGE_WEEK_CENTER);
  +            it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_CENTER);
               assertWeekIterator(it, centered);
               now.add(Calendar.DATE,1);
           }
       }
  -
  +            
       /**
        * Tests the calendar iterator for month-based ranges
        */
       public void testMonthIterator() throws Exception {
  -        Iterator it = DateUtils.getCalendarIterator(date1, DateUtils.RANGE_MONTH_SUNDAY);
  +        Iterator it = DateUtils.iterator(date1, DateUtils.RANGE_MONTH_SUNDAY);
           assertWeekIterator(it,
                   dateParser.parse("January 27, 2002"),
                   dateParser.parse("March 2, 2002"));
   
  -        it = DateUtils.getCalendarIterator(date1, DateUtils.RANGE_MONTH_MONDAY);
  +        it = DateUtils.iterator(date1, DateUtils.RANGE_MONTH_MONDAY);
           assertWeekIterator(it,
                   dateParser.parse("January 28, 2002"),
                   dateParser.parse("March 3, 2002"));
   
  -        it = DateUtils.getCalendarIterator(date2, DateUtils.RANGE_MONTH_SUNDAY);
  +        it = DateUtils.iterator(date2, DateUtils.RANGE_MONTH_SUNDAY);
           assertWeekIterator(it,
                   dateParser.parse("October 28, 2001"),
                   dateParser.parse("December 1, 2001"));
   
  -        it = DateUtils.getCalendarIterator(date2, DateUtils.RANGE_MONTH_MONDAY);
  +        it = DateUtils.iterator(date2, DateUtils.RANGE_MONTH_MONDAY);
           assertWeekIterator(it,
                   dateParser.parse("October 29, 2001"),
                   dateParser.parse("December 2, 2001"));
  @@ -355,7 +419,7 @@
           int count = 1;
           while (it.hasNext()) {
               //Check this is just a date (no time component)
  -            assertEquals("", cal, DateUtils.trunc(cal, Calendar.DATE), 0);
  +            assertEquals("", cal, DateUtils.truncate(cal, Calendar.DATE), 0);
   
               last = cal;
               cal = (Calendar) it.next();
  
  
  
  1.4       +229 -90   jakarta-commons/lang/src/java/org/apache/commons/lang/time/DateUtils.java
  
  Index: DateUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/time/DateUtils.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DateUtils.java	8 Jun 2003 23:14:23 -0000	1.3
  +++ DateUtils.java	23 Jun 2003 23:41:10 -0000	1.4
  @@ -151,11 +151,35 @@
       public final static int RANGE_MONTH_MONDAY = 6;
   
       /**
  -     * See the other round method.  Works with a Date object.
  +     * <p><code>DateUtils<code> instances should NOT be constructed in
  +     * standard programming. Instead, the class should be used as
  +     * <code>DateUtils.parse(str);</code>.</p>
  +     *
  +     * <p>This constructor is public to permit tools that require a JavaBean
  +     * instance to operate.</p>
        */
  -    public static Date round(Date val, int field) {
  +    public DateUtils() {
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    /**
  +     * Round this date, leaving the field specified as the most significant
  +     * field.  For example, if you had the datetime of 28 Mar 2002
  +     * 13:45:01.231, if this was passed with HOUR, it would return 28 Mar
  +     * 2002 14:00:00.000.  If this was passed with MONTH, it would return
  +     * 1 April 2002 0:00:00.000.
  +     * 
  +     * @param date  the date to work with
  +     * @param field  the field from <code>Calendar</code> or SEMI_MONTH
  +     * @return the rounded date
  +     * @throws IllegalArgumentException if the date is null
  +     */
  +    public static Date round(Date date, int field) {
  +        if (date == null) {
  +            throw new IllegalArgumentException("The date must not be null");
  +        }
           GregorianCalendar gval = new GregorianCalendar();
  -        gval.setTime(val);
  +        gval.setTime(date);
           modify(gval, field, true);
           return gval.getTime();
       }
  @@ -166,33 +190,66 @@
        * 13:45:01.231, if this was passed with HOUR, it would return 28 Mar
        * 2002 14:00:00.000.  If this was passed with MONTH, it would return
        * 1 April 2002 0:00:00.000.
  -     */
  -    public static Calendar round(Calendar val, int field) {
  -        Calendar rounded = (Calendar) val.clone();
  +     * 
  +     * @param date  the date to work with
  +     * @param field  the field from <code>Calendar</code> or SEMI_MONTH
  +     * @return the rounded date (a different object)
  +     * @throws IllegalArgumentException if the date is null
  +     */
  +    public static Calendar round(Calendar date, int field) {
  +        if (date == null) {
  +            throw new IllegalArgumentException("The date must not be null");
  +        }
  +        Calendar rounded = (Calendar) date.clone();
           modify(rounded, field, true);
           return rounded;
       }
   
       /**
  -     * See the other round method.  Works with an Object, trying to
  -     * use it as either a Date or Calendar.
  -     */
  -    public static Date round(Object val, int field) {
  -        if (val instanceof Date) {
  -            return round((Date) val, field);
  -        } else if (val instanceof Calendar) {
  -            return round((Calendar) val, field).getTime();
  +     * Round this date, leaving the field specified as the most significant
  +     * field.  For example, if you had the datetime of 28 Mar 2002
  +     * 13:45:01.231, if this was passed with HOUR, it would return 28 Mar
  +     * 2002 14:00:00.000.  If this was passed with MONTH, it would return
  +     * 1 April 2002 0:00:00.000.
  +     * 
  +     * @param date  the date to work with, either Date or Calendar
  +     * @param field  the field from <code>Calendar</code> or SEMI_MONTH
  +     * @return the rounded date
  +     * @throws IllegalArgumentException if the date is null
  +     * @throws ClassCastException if the object type is not a Date or Calendar
  +     */
  +    public static Date round(Object date, int field) {
  +        if (date == null) {
  +            throw new IllegalArgumentException("The date must not be null");
  +        }
  +        if (date instanceof Date) {
  +            return round((Date) date, field);
  +        } else if (date instanceof Calendar) {
  +            return round((Calendar) date, field).getTime();
           } else {
  -            throw new ClassCastException("Could not round " + val);
  +            throw new ClassCastException("Could not round " + date);
           }
       }
   
  +    //-----------------------------------------------------------------------
       /**
  -     * See the other trunc method.  Works with a Date.
  -     */
  -    public static Date trunc(Date val, int field) {
  +     * Truncate this date, leaving the field specified as the most significant
  +     * field.  For example, if you had the datetime of 28 Mar 2002
  +     * 13:45:01.231, if you passed with HOUR, it would return 28 Mar
  +     * 2002 13:00:00.000.  If this was passed with MONTH, it would return
  +     * 1 Mar 2002 0:00:00.000.
  +     * 
  +     * @param date  the date to work with
  +     * @param field  the field from <code>Calendar</code> or SEMI_MONTH
  +     * @return the rounded date
  +     * @throws IllegalArgumentException if the date is null
  +     */
  +    public static Date truncate(Date date, int field) {
  +        if (date == null) {
  +            throw new IllegalArgumentException("The date must not be null");
  +        }
           GregorianCalendar gval = new GregorianCalendar();
  -        gval.setTime(val);
  +        gval.setTime(date);
           modify(gval, field, false);
           return gval.getTime();
       }
  @@ -203,27 +260,55 @@
        * 13:45:01.231, if you passed with HOUR, it would return 28 Mar
        * 2002 13:00:00.000.  If this was passed with MONTH, it would return
        * 1 Mar 2002 0:00:00.000.
  -     */
  -    public static Calendar trunc(Calendar val, int field) {
  -        Calendar truncated = (Calendar) val.clone();
  +     * 
  +     * @param date  the date to work with
  +     * @param field  the field from <code>Calendar</code> or SEMI_MONTH
  +     * @return the rounded date (a different object)
  +     * @throws IllegalArgumentException if the date is null
  +     */
  +    public static Calendar truncate(Calendar date, int field) {
  +        if (date == null) {
  +            throw new IllegalArgumentException("The date must not be null");
  +        }
  +        Calendar truncated = (Calendar) date.clone();
           modify(truncated, field, false);
           return truncated;
       }
   
       /**
  -     * See the other trunc method.  Works with an Object, trying to
  -     * use it as either a Date or Calendar.
  -     */
  -    public static Date trunc(Object val, int field) {
  -        if (val instanceof Date) {
  -            return trunc((Date) val, field);
  -        } else if (val instanceof Calendar) {
  -            return trunc((Calendar) val, field).getTime();
  +     * Truncate this date, leaving the field specified as the most significant
  +     * field.  For example, if you had the datetime of 28 Mar 2002
  +     * 13:45:01.231, if you passed with HOUR, it would return 28 Mar
  +     * 2002 13:00:00.000.  If this was passed with MONTH, it would return
  +     * 1 Mar 2002 0:00:00.000.
  +     * 
  +     * @param date  the date to work with, either Date or Calendar
  +     * @param field  the field from <code>Calendar</code> or SEMI_MONTH
  +     * @return the rounded date
  +     * @throws IllegalArgumentException if the date is null
  +     * @throws ClassCastException if the object type is not a Date or Calendar
  +     */
  +    public static Date truncate(Object date, int field) {
  +        if (date == null) {
  +            throw new IllegalArgumentException("The date must not be null");
  +        }
  +        if (date instanceof Date) {
  +            return truncate((Date) date, field);
  +        } else if (date instanceof Calendar) {
  +            return truncate((Calendar) date, field).getTime();
           } else {
  -            throw new ClassCastException("Could not trunc " + val);
  +            throw new ClassCastException("Could not truncate " + date);
           }
       }
   
  +    //-----------------------------------------------------------------------
  +    /**
  +     * Internal calculation method
  +     * 
  +     * @param val  the calendar
  +     * @param field  the field constant
  +     * @param round  true to round, false to truncate
  +     */
       private static void modify(Calendar val, int field, boolean round) {
           boolean roundUp = false;
           for (int i = 0; i < fields.length; i++) {
  @@ -295,26 +380,39 @@
               //We need to remove this field
               val.add(fields[i][0], -offset);
           }
  -        throw new RuntimeException("We do not support that field.");
  +        throw new IllegalArgumentException("The field " + field + " is not supported");
   
       }
   
  +    //-----------------------------------------------------------------------
       /**
  -     * Parses strings the way that CVS supports it (very human readable).
  +     * Parses a date string formatted in CVS format.
  +     * 
  +     * @param dateStr  the date to parse
  +     * @return the parsed date
  +     * @throws IllegalArgumentException if the date cannot be parsed
        */
  -    public static Calendar parse(String original) {
  -        return parse(original, Locale.getDefault());
  +    public static Calendar parseCVS(String dateStr) {
  +        return parseCVS(dateStr, Locale.getDefault());
       }
   
       /**
  -     * Parses strings the way that CVS supports it (very human readable).
  -     */
  -    public static Calendar parse(String original, Locale locale) {
  +     * Parses a date string formatted in CVS format.
  +     * 
  +     * @param dateStr  the date to parse
  +     * @param locale  the locale to parse in
  +     * @return the parsed date
  +     * @throws IllegalArgumentException if the date is null or cannot be parsed
  +     */
  +    public static Calendar parseCVS(String dateStr, Locale locale) {
  +        if (dateStr == null) {
  +            throw new IllegalArgumentException("The date must not be null");
  +        }
           //Get the symbol names
           DateFormatSymbols symbols = new DateFormatSymbols(locale);
   
           //Prep the string to parse
  -        String value = original.toLowerCase().trim();
  +        String value = dateStr.toLowerCase().trim();
   
           //Get the current date/time
           Calendar now = Calendar.getInstance();
  @@ -326,7 +424,7 @@
               //Split the value and unit
               int start = value.indexOf(" ");
               if (start < 0) {
  -                throw new RuntimeException("Could not find space in between value and unit");
  +                throw new IllegalArgumentException("Could not find space in between value and unit");
               }
               String unit = value.substring(start + 1);
               value = value.substring(0, start);
  @@ -358,7 +456,7 @@
               } else if (unit.equals("years") || unit.equals("year")) {
                   now.add(Calendar.YEAR, -val);
               } else {
  -                throw new RuntimeException("We do not understand that many units ago");
  +                throw new IllegalArgumentException("We do not understand that many units ago");
               }
               return now;
           } else if (value.startsWith("last ")) {
  @@ -404,7 +502,7 @@
           //Try to parse the date a number of different ways
           for (int i = 0; i < dateFormats.length; i++) {
               try {
  -                Date datetime = dateFormats[i].parse(original);
  +                Date datetime = dateFormats[i].parse(dateStr);
                   Calendar cal = Calendar.getInstance();
                   cal.setTime(datetime);
                   return cal;
  @@ -413,17 +511,47 @@
               }
           }
   
  -        throw new RuntimeException("Unable to parse '" + original + "'.");
  +        throw new IllegalArgumentException("Unable to parse '" + dateStr + "'.");
       }
   
  +    //-----------------------------------------------------------------------
       /**
        * This constructs an Iterator that will start and stop over a date
        * range based on the focused date and the range style.  For instance,
        * passing Thursday, July 4, 2002 and a RANGE_MONTH_SUNDAY will return
        * an Iterator that starts with Sunday, June 30, 2002 and ends with
        * Saturday, August 3, 2002.
  -     */
  -    public static Iterator getCalendarIterator(Calendar focus, int rangeStyle) {
  +     * 
  +     * @param focus  the date to work with
  +     * @param rangeStyle  the style constant to use
  +     * @return the date iterator
  +     * @throws IllegalArgumentException if the date is null
  +     */
  +    public static Iterator iterator(Date focus, int rangeStyle) {
  +        if (focus == null) {
  +            throw new IllegalArgumentException("The date must not be null");
  +        }
  +        GregorianCalendar gval = new GregorianCalendar();
  +        gval.setTime(focus);
  +        return iterator(gval, rangeStyle);
  +    }
  +
  +    /**
  +     * This constructs an Iterator that will start and stop over a date
  +     * range based on the focused date and the range style.  For instance,
  +     * passing Thursday, July 4, 2002 and a RANGE_MONTH_SUNDAY will return
  +     * an Iterator that starts with Sunday, June 30, 2002 and ends with
  +     * Saturday, August 3, 2002.
  +     * 
  +     * @param focus  the date to work with
  +     * @param rangeStyle  the style constant to use
  +     * @return the date iterator
  +     * @throws IllegalArgumentException if the date is null
  +     */
  +    public static Iterator iterator(Calendar focus, int rangeStyle) {
  +        if (focus == null) {
  +            throw new IllegalArgumentException("The date must not be null");
  +        }
           Calendar start = null;
           Calendar end = null;
           int startCutoff = Calendar.SUNDAY;
  @@ -432,7 +560,7 @@
               case RANGE_MONTH_SUNDAY:
               case RANGE_MONTH_MONDAY:
                   //Set start to the first of the month
  -                start = trunc(focus, Calendar.MONTH);
  +                start = truncate(focus, Calendar.MONTH);
                   //Set end to the last of the month
                   end = (Calendar) start.clone();
                   end.add(Calendar.MONTH, 1);
  @@ -448,8 +576,8 @@
               case RANGE_WEEK_RELATIVE:
               case RANGE_WEEK_CENTER:
                   //Set start and end to the current date
  -                start = trunc(focus, Calendar.DATE);
  -                end = trunc(focus, Calendar.DATE);
  +                start = truncate(focus, Calendar.DATE);
  +                end = truncate(focus, Calendar.DATE);
                   switch (rangeStyle) {
                       case RANGE_WEEK_SUNDAY:
                           //already set by default
  @@ -469,7 +597,7 @@
                   }
                   break;
               default:
  -                throw new RuntimeException("The range style " + rangeStyle + " is not valid.");
  +                throw new IllegalArgumentException("The range style " + rangeStyle + " is not valid.");
           }
           if (startCutoff < Calendar.SUNDAY) {
               startCutoff += 7;
  @@ -489,54 +617,65 @@
           while (end.get(Calendar.DAY_OF_WEEK) != endCutoff) {
               end.add(Calendar.DATE, 1);
           }
  -        final Calendar startFinal = start;
  -        final Calendar endFinal = end;
  -        Iterator it = new Iterator() {
  -            Calendar spot = null;
  -            {
  -                spot = startFinal;
  -                spot.add(Calendar.DATE, -1);
  -            }
  -
  -            public boolean hasNext() {
  -                return spot.before(endFinal);
  -            }
  -
  -            public Object next() {
  -                if (spot.equals(endFinal)) {
  -                    throw new NoSuchElementException();
  -                }
  -                spot.add(Calendar.DATE, 1);
  -                return spot.clone();
  -            }
  -
  -            public void remove() {
  -                throw new UnsupportedOperationException();
  -            }
  -        };
  -        return it;
  -    }
  -
  -    /**
  -     * See the other getCalendarIterator.  Works with a Date.
  -     */
  -    public static Iterator getCalendarIterator(Date focus, int rangeStyle) {
  -        GregorianCalendar gval = new GregorianCalendar();
  -        gval.setTime(focus);
  -        return getCalendarIterator(gval, rangeStyle);
  +        return new DateIterator(start, end);
       }
   
       /**
  -     * See the other getCalendarIterator.  Works with an Object, trying
  -     * to use it as a Date or Calendar.
  -     */
  -    public static Iterator getCalendarIterator(Object focus, int rangeStyle) {
  +     * This constructs an Iterator that will start and stop over a date
  +     * range based on the focused date and the range style.  For instance,
  +     * passing Thursday, July 4, 2002 and a RANGE_MONTH_SUNDAY will return
  +     * an Iterator that starts with Sunday, June 30, 2002 and ends with
  +     * Saturday, August 3, 2002.
  +     * 
  +     * @param focus  the date to work with, either Date or Calendar
  +     * @param rangeStyle  the style constant to use
  +     * @return the date iterator
  +     * @throws IllegalArgumentException if the date is null
  +     * @throws ClassCastException if the object type is not a Date or Calendar
  +     */
  +    public static Iterator iterator(Object focus, int rangeStyle) {
  +        if (focus == null) {
  +            throw new IllegalArgumentException("The date must not be null");
  +        }
           if (focus instanceof Date) {
  -            return getCalendarIterator((Date) focus, rangeStyle);
  +            return iterator((Date) focus, rangeStyle);
           } else if (focus instanceof Calendar) {
  -            return getCalendarIterator((Calendar) focus, rangeStyle);
  +            return iterator((Calendar) focus, rangeStyle);
           } else {
               throw new ClassCastException("Could not iterate based on " + focus);
  +        }
  +    }
  +
  +    /**
  +     * Date iterator.
  +     */
  +    static class DateIterator implements Iterator {
  +        private final Calendar startFinal;
  +        private final Calendar endFinal;
  +        private Calendar spot = null;
  +        
  +        DateIterator(Calendar startFinal, Calendar endFinal) {
  +            super();
  +            this.startFinal = startFinal;
  +            this.endFinal = endFinal;
  +            spot = startFinal;
  +            spot.add(Calendar.DATE, -1);
  +        }
  +
  +        public boolean hasNext() {
  +            return spot.before(endFinal);
  +        }
  +
  +        public Object next() {
  +            if (spot.equals(endFinal)) {
  +                throw new NoSuchElementException();
  +            }
  +            spot.add(Calendar.DATE, 1);
  +            return spot.clone();
  +        }
  +
  +        public void remove() {
  +            throw new UnsupportedOperationException();
           }
       }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org