commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/lang RELEASE-NOTES.txt
Date Sat, 16 Oct 2004 21:06:18 GMT
scolebourne    2004/10/16 14:06:18

  Modified:    lang/src/test/org/apache/commons/lang/time
                        DateUtilsTest.java
               lang/src/java/org/apache/commons/lang/time DateUtils.java
               lang     RELEASE-NOTES.txt
  Log:
  Add parse method that handles multiple patterns
  bug 30674
  
  Revision  Changes    Path
  1.21      +35 -0     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.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- DateUtilsTest.java	16 Oct 2004 17:43:05 -0000	1.20
  +++ DateUtilsTest.java	16 Oct 2004 21:06:18 -0000	1.21
  @@ -18,6 +18,7 @@
   import java.lang.reflect.Constructor;
   import java.lang.reflect.Modifier;
   import java.text.DateFormat;
  +import java.text.ParseException;
   import java.text.SimpleDateFormat;
   import java.util.Calendar;
   import java.util.Date;
  @@ -235,6 +236,40 @@
           assertEquals(false, DateUtils.isSameLocalTime(cal1, cal2));
           try {
               DateUtils.isSameLocalTime((Calendar) null, (Calendar) null);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +    }
  +    
  +    //-----------------------------------------------------------------------
  +    public void testParseDate() throws Exception {
  +        GregorianCalendar cal = new GregorianCalendar(1972, 11, 3);
  +        String dateStr = "1972-12-03";
  +        String[] parsers = new String[] {"yyyy'-'DDD", "yyyy'-'MM'-'dd", "yyyyMMdd"};
  +        Date date = DateUtils.parseDate(dateStr, parsers);
  +        assertEquals(cal.getTime(), date);
  +        
  +        dateStr = "1972-338";
  +        date = DateUtils.parseDate(dateStr, parsers);
  +        assertEquals(cal.getTime(), date);
  +        
  +        dateStr = "19721203";
  +        date = DateUtils.parseDate(dateStr, parsers);
  +        assertEquals(cal.getTime(), date);
  +        
  +        try {
  +            DateUtils.parseDate("PURPLE", parsers);
  +            fail();
  +        } catch (ParseException ex) {}
  +        try {
  +            DateUtils.parseDate("197212AB", parsers);
  +            fail();
  +        } catch (ParseException ex) {}
  +        try {
  +            DateUtils.parseDate(null, parsers);
  +            fail();
  +        } catch (IllegalArgumentException ex) {}
  +        try {
  +            DateUtils.parseDate(dateStr, null);
               fail();
           } catch (IllegalArgumentException ex) {}
       }
  
  
  
  1.35      +40 -1     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.34
  retrieving revision 1.35
  diff -u -r1.34 -r1.35
  --- DateUtils.java	16 Oct 2004 17:43:05 -0000	1.34
  +++ DateUtils.java	16 Oct 2004 21:06:18 -0000	1.35
  @@ -15,6 +15,9 @@
    */
   package org.apache.commons.lang.time;
   
  +import java.text.ParseException;
  +import java.text.ParsePosition;
  +import java.text.SimpleDateFormat;
   import java.util.Calendar;
   import java.util.Date;
   import java.util.Iterator;
  @@ -226,6 +229,42 @@
                   cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                   cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
                   cal1.getClass() == cal2.getClass());
  +    }
  +
  +    //-----------------------------------------------------------------------
  +    /**
  +     * <p>Parses a string representing a date by trying a variety of different parsers.</p>
  +     * 
  +     * <p>The parse will try each parse pattern in turn.
  +     * A parse is only deemed sucessful if it parses the whole of the input string.
  +     * If no parse patterns match, a ParseException is thrown.</p>
  +     * 
  +     * @param str  the date to parse, not null
  +     * @param parsePatterns  the date format patterns to use, see SimpleDateFormat, not
null
  +     * @return the parsed date
  +     * @throws IllegalArgumentException if the date string or pattern array is null
  +     * @throws ParseException if none of the date patterns were suitable
  +     */
  +    public static Date parseDate(String str, String[] parsePatterns) throws ParseException
{
  +        if (str == null || parsePatterns == null) {
  +            throw new IllegalArgumentException("Date and Patterns must not be null");
  +        }
  +        
  +        SimpleDateFormat parser = null;
  +        ParsePosition pos = new ParsePosition(0);
  +        for (int i = 0; i < parsePatterns.length; i++) {
  +            if (i == 0) {
  +                parser = new SimpleDateFormat(parsePatterns[0]);
  +            } else {
  +                parser.applyPattern(parsePatterns[i]);
  +            }
  +            pos.setIndex(0);
  +            Date date = parser.parse(str, pos);
  +            if (date != null && pos.getIndex() == str.length()) {
  +                return date;
  +            }
  +        }
  +        throw new ParseException("Unable to parse the date: " + str, -1);
       }
   
       //-----------------------------------------------------------------------
  
  
  
  1.33      +3 -1      jakarta-commons/lang/RELEASE-NOTES.txt
  
  Index: RELEASE-NOTES.txt
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/RELEASE-NOTES.txt,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- RELEASE-NOTES.txt	16 Oct 2004 18:52:21 -0000	1.32
  +++ RELEASE-NOTES.txt	16 Oct 2004 21:06:18 -0000	1.33
  @@ -75,6 +75,7 @@
   - NumberUtils - various string to number parsing methods added
   
   - DateUtils - methods added to compare dates in various ways
  +           -- method to parse a date string using multiple patterns
   - FastDateFormat - extra formatting methods that take in a millisecond long value
                   -- additional static factory methods
   - StopWatch - new methods for split behaviour
  @@ -113,6 +114,7 @@
   29794  Add convenience format(long) methods to FastDateForma
   30328  HashCodeBuilder does not use the same values as Boolean (fixed as documentation)
   30334  New class proposal: CharacterEncoding
  +30674  parseDate class from HttpClient's DateParser class
   30815  ArrayUtils.isEquals() throws ClassCastException when array1
   30929  Nestable.indexOfThrowable(Class)  uses Class.equals() to match
   31395  DateUtils.truncate oddity at the far end of the Date spectrum
  
  
  

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


Mime
View raw message