Add parse method that handles multiple patterns
bug 30674 git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137989 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
e08b9b890f
commit
ac517f7c0d
|
@ -1,4 +1,4 @@
|
|||
$Id: RELEASE-NOTES.txt,v 1.32 2004/10/16 18:52:21 scolebourne Exp $
|
||||
$Id: RELEASE-NOTES.txt,v 1.33 2004/10/16 21:06:18 scolebourne Exp $
|
||||
|
||||
Commons Lang Package
|
||||
Version 2.1
|
||||
|
@ -75,6 +75,7 @@ indexOfThrowable method untouched (see incompatible changes section)
|
|||
- 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 @@ BUG FIXES:
|
|||
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
|
||||
|
|
|
@ -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;
|
||||
|
@ -31,7 +34,7 @@
|
|||
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
|
||||
* @author Phil Steitz
|
||||
* @since 2.0
|
||||
* @version $Id: DateUtils.java,v 1.34 2004/10/16 17:43:05 scolebourne Exp $
|
||||
* @version $Id: DateUtils.java,v 1.35 2004/10/16 21:06:18 scolebourne Exp $
|
||||
*/
|
||||
public class DateUtils {
|
||||
|
||||
|
@ -228,6 +231,42 @@ public static boolean isSameLocalTime(Calendar cal1, Calendar cal2) {
|
|||
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);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Round this date, leaving the field specified as the most
|
||||
|
|
|
@ -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;
|
||||
|
@ -239,6 +240,40 @@ public void testIsSameLocalTime_Cal() {
|
|||
} 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) {}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Tests various values with the round method
|
||||
|
|
Loading…
Reference in New Issue