Author: niallp
Date: Sat Jan 30 17:58:29 2010
New Revision: 904835
URL: http://svn.apache.org/viewvc?rev=904835&view=rev
Log:
Port LANG-486 to 2.x branch - add parseDateStrictly() method method to DateUtils
Modified:
commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/time/DateUtils.java
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/time/DateUtilsTest.java
Modified:
commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/time/DateUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/time/DateUtils.java?rev=904835&r1=904834&r2=904835&view=diff
==============================================================================
---
commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/time/DateUtils.java
(original)
+++
commons/proper/lang/branches/LANG_2_X/src/main/java/org/apache/commons/lang/time/DateUtils.java
Sat Jan 30 17:58:29 2010
@@ -274,16 +274,42 @@
* <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
* @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
+ * @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");
}
@@ -301,6 +327,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/branches/LANG_2_X/src/test/java/org/apache/commons/lang/time/DateUtilsTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/time/DateUtilsTest.java?rev=904835&r1=904834&r2=904835&view=diff
==============================================================================
---
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/time/DateUtilsTest.java
(original)
+++
commons/proper/lang/branches/LANG_2_X/src/test/java/org/apache/commons/lang/time/DateUtilsTest.java
Sat Jan 30 17:58:29 2010
@@ -283,6 +283,24 @@
DateUtils.parseDate(dateStr, null);
fail();
} catch (IllegalArgumentException ex) {}
+ try {
+ DateUtils.parseDate(dateStr, new String[0]);
+ 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) {}
}
//-----------------------------------------------------------------------