Index: java/org/apache/commons/httpclient/util/DateUtil.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/DateUtil.java,v retrieving revision 1.1 diff -u -r1.1 DateUtil.java --- java/org/apache/commons/httpclient/util/DateUtil.java 6 Nov 2004 19:15:42 -0000 1.1 +++ java/org/apache/commons/httpclient/util/DateUtil.java 23 Dec 2004 22:13:50 -0000 @@ -32,6 +32,7 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Arrays; +import java.util.Calendar; import java.util.Collection; import java.util.Date; import java.util.Iterator; @@ -73,6 +74,14 @@ private static final Collection DEFAULT_PATTERNS = Arrays.asList( new String[] { PATTERN_ASCTIME, PATTERN_RFC1036, PATTERN_RFC1123 } ); + private static final Date DEFAULT_TWO_DIGIT_YEAR_START; + + static { + Calendar calendar = Calendar.getInstance(); + calendar.set(2000, Calendar.JANUARY, 1, 0, 0); + DEFAULT_TWO_DIGIT_YEAR_START = calendar.getTime(); + } + /** * Parses a date value. The formats used for parsing the date value are retrieved from * the default http params. @@ -85,7 +94,22 @@ * supported date formats */ public static Date parseDate(String dateValue) throws DateParseException { - return parseDate(dateValue, null); + return parseDate(dateValue, null, null); + } + + /** + * Parses the date value using the given date formats. + * + * @param dateValue the date value to parse + * @param dateFormats the date formats to use + * + * @return the parsed date + * + * @throws DateParseException if none of the dataFormats could parse the dateValue + */ + public static Date parseDate(String dateValue, Collection dateFormats) + throws DateParseException { + return parseDate(dateValue, dateFormats, null); } /** @@ -93,6 +117,10 @@ * * @param dateValue the date value to parse * @param dateFormats the date formats to use + * @param startDate During parsing, two digit years will be placed in the range + * startDate to startDate + 100 years. This value may + * be null. When null is given as a parameter, year + * 2000 will be used. * * @return the parsed date * @@ -100,7 +128,8 @@ */ public static Date parseDate( String dateValue, - Collection dateFormats + Collection dateFormats, + Date startDate ) throws DateParseException { if (dateValue == null) { @@ -109,6 +138,9 @@ if (dateFormats == null) { dateFormats = DEFAULT_PATTERNS; } + if (startDate == null) { + startDate = DEFAULT_TWO_DIGIT_YEAR_START; + } // trim single quotes around date if present // see http://nagoya.apache.org/bugzilla/show_bug.cgi?id=5279 if (dateValue.length() > 1 @@ -126,6 +158,7 @@ if (dateParser == null) { dateParser = new SimpleDateFormat(format, Locale.US); dateParser.setTimeZone(TimeZone.getTimeZone("GMT")); + dateParser.set2DigitYearStart(startDate); } else { dateParser.applyPattern(format); } Index: test/org/apache/commons/httpclient/cookie/TestDateParser.java =================================================================== RCS file: test/org/apache/commons/httpclient/cookie/TestDateParser.java diff -N test/org/apache/commons/httpclient/cookie/TestDateParser.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test/org/apache/commons/httpclient/cookie/TestDateParser.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,92 @@ +/* + * $Header$ + * $Revision$ + * $Date$ + * ==================================================================== + * + * Copyright 1999-2004 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.commons.httpclient.cookie; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +import org.apache.commons.httpclient.util.DateUtil; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + + +/** + * Test cases for expiry date parsing + * + * @author Oleg Kalnichevski + * + * @version $Revision$ + */ +public class TestDateParser extends TestCase { + + // ------------------------------------------------------------ Constructor + + public TestDateParser(String name) { + super(name); + } + + // ------------------------------------------------------- TestCase Methods + + public static Test suite() { + return new TestSuite(TestDateParser.class); + } + + private static final String PATTERN = "EEE, dd-MMM-yy HH:mm:ss zzz"; + private static final List PATTERNS = new ArrayList(); + + static { + PATTERNS.add(PATTERN); + } + + public void testFourDigitYear() throws Exception { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(DateUtil.parseDate("Thu, 23-Dec-2004 24:00:00 CET", PATTERNS)); + assertEquals(2004, calendar.get(Calendar.YEAR)); + } + + public void testThreeDigitYear() throws Exception { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(DateUtil.parseDate("Thu, 23-Dec-994 24:00:00 CET", PATTERNS)); + assertEquals(994, calendar.get(Calendar.YEAR)); + } + + public void testTwoDigitYear() throws Exception { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(DateUtil.parseDate("Thu, 23-Dec-04 24:00:00 CET", PATTERNS)); + assertEquals(2004, calendar.get(Calendar.YEAR)); + + calendar.setTime(DateUtil.parseDate("Thu, 23-Dec-94 24:00:00 CET", PATTERNS)); + assertEquals(2094, calendar.get(Calendar.YEAR)); + } + +} +