Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 92756 invoked from network); 30 Jan 2010 17:58:54 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 30 Jan 2010 17:58:54 -0000 Received: (qmail 76294 invoked by uid 500); 30 Jan 2010 17:58:53 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 76192 invoked by uid 500); 30 Jan 2010 17:58:53 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 76183 invoked by uid 99); 30 Jan 2010 17:58:53 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 30 Jan 2010 17:58:53 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 30 Jan 2010 17:58:51 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id ECCBB2388980; Sat, 30 Jan 2010 17:58:29 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r904835 - in /commons/proper/lang/branches/LANG_2_X/src: main/java/org/apache/commons/lang/time/DateUtils.java test/java/org/apache/commons/lang/time/DateUtilsTest.java Date: Sat, 30 Jan 2010 17:58:29 -0000 To: commits@commons.apache.org From: niallp@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100130175829.ECCBB2388980@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 @@ *

Parses a string representing a date by trying a variety of different parsers.

* *

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.

+ * 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); + } + + //----------------------------------------------------------------------- + /** + *

Parses a string representing a date by trying a variety of different parsers.

+ * + *

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.

+ * 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) {} } //-----------------------------------------------------------------------