You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2010/01/01 20:01:18 UTC

svn commit: r895055 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang3/time/DateUtils.java test/org/apache/commons/lang3/time/DateUtilsTest.java

Author: bayard
Date: Fri Jan  1 19:01:18 2010
New Revision: 895055

URL: http://svn.apache.org/viewvc?rev=895055&view=rev
Log:
Adding parseDateStrictly method per LANG-486

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang3/time/DateUtils.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang3/time/DateUtilsTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang3/time/DateUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang3/time/DateUtils.java?rev=895055&r1=895054&r2=895055&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang3/time/DateUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang3/time/DateUtils.java Fri Jan  1 19:01:18 2010
@@ -273,8 +273,9 @@
      * <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.
+     * A parse is only deemed successful if it parses the whole of the input string.
      * If no parse patterns match, a ParseException is thrown.</p>
+     * The parser will be lenient toward the parsed date.
      * 
      * @param str  the date to parse, not null
      * @param parsePatterns  the date format patterns to use, see SimpleDateFormat, not null
@@ -283,6 +284,31 @@
      * @throws ParseException if none of the date patterns were suitable (or there were none)
      */
     public static Date parseDate(String str, String[] parsePatterns) throws ParseException {
+        return parseDateWithLeniency(str, parsePatterns, true);
+    }
+    
+  //-----------------------------------------------------------------------
+    /**
+     * <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 successful if it parses the whole of the input string.
+     * If no parse patterns match, a ParseException is thrown.</p>
+     * The parser parses strictly - it does not allow for dates such as "February 942, 1996". 
+     * 
+     * @param str  the date to parse, not null
+     * @param parsePatterns  the date format patterns to use, see SimpleDateFormat, not null
+     * @param lenient Specify whether or not date/time parsing is to be lenient.
+     * @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
+     * @see java.util.Calender#isLenient()
+     */
+    public static Date parseDateStrictly(String str, String[] parsePatterns) throws ParseException {
+        return parseDateWithLeniency(str, parsePatterns, false);
+    }
+    private static Date parseDateWithLeniency(String str, String[] parsePatterns,
+			boolean lenient) throws ParseException {
         if (str == null || parsePatterns == null) {
             throw new IllegalArgumentException("Date and Patterns must not be null");
         }
@@ -300,6 +326,7 @@
             
             if (i == 0) {
                 parser = new SimpleDateFormat(pattern);
+                parser.setLenient(lenient);
             } else {
                 parser.applyPattern(pattern); // cannot be null if i != 0
             }

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang3/time/DateUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang3/time/DateUtilsTest.java?rev=895055&r1=895054&r2=895055&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang3/time/DateUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang3/time/DateUtilsTest.java Fri Jan  1 19:01:18 2010
@@ -272,6 +272,20 @@
             fail();
         } catch (ParseException ex) {}
     }
+    // LANG-486
+    public void testParseDateWithLeniency() throws Exception {
+        GregorianCalendar cal = new GregorianCalendar(1998, 6, 30);
+        String dateStr = "February 942, 1996";
+        String[] parsers = new String[] {"MMMMM DDD, yyyy"};
+        
+        Date date = DateUtils.parseDate(dateStr, parsers);
+        assertEquals(cal.getTime(), date);
+        
+        try {
+            date = DateUtils.parseDateStrictly(dateStr, parsers);
+            fail();
+        } catch (ParseException ex) {}
+    }
 
     //-----------------------------------------------------------------------
     public void testAddYears() throws Exception {