1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/DateParser.java,v 1.10 2004/09/14 20:11:32 olegk Exp $
  3. * $Revision: 1.10 $
  4. * $Date: 2004/09/14 20:11:32 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 1999-2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. */
  29. package org.apache.commons.httpclient.util;
  30. import java.text.ParseException;
  31. import java.text.SimpleDateFormat;
  32. import java.util.Arrays;
  33. import java.util.Collection;
  34. import java.util.Date;
  35. import java.util.Iterator;
  36. import java.util.Locale;
  37. import java.util.TimeZone;
  38. import org.apache.commons.logging.Log;
  39. import org.apache.commons.logging.LogFactory;
  40. /**
  41. * A utility class for parsing HTTP dates as used in cookies and other headers.
  42. * This class handles dates as defined by RFC 2616 section 3.3.1 as well as
  43. * some other common non-standard formats.
  44. *
  45. * @author Christopher Brown
  46. * @author Michael Becke
  47. */
  48. public class DateParser {
  49. /** Log object for this class. */
  50. private static final Log LOG = LogFactory.getLog(DateParser.class);
  51. /**
  52. * Date format pattern used to parse HTTP date headers in RFC 1123 format.
  53. */
  54. public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
  55. /**
  56. * Date format pattern used to parse HTTP date headers in RFC 1036 format.
  57. */
  58. public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz";
  59. /**
  60. * Date format pattern used to parse HTTP date headers in ANSI C
  61. * <code>asctime()</code> format.
  62. */
  63. public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
  64. private static final Collection DEFAULT_PATTERNS = Arrays.asList(
  65. new String[] { PATTERN_ASCTIME, PATTERN_RFC1036, PATTERN_RFC1123 } );
  66. /**
  67. * Parses a date value. The formats used for parsing the date value are retrieved from
  68. * the default http params.
  69. *
  70. * @param dateValue the date value to parse
  71. *
  72. * @return the parsed date
  73. *
  74. * @throws DateParseException if the value could not be parsed using any of the
  75. * supported date formats
  76. */
  77. public static Date parseDate(String dateValue) throws DateParseException {
  78. return parseDate(dateValue, null);
  79. }
  80. /**
  81. * Parses the date value using the given date formats.
  82. *
  83. * @param dateValue the date value to parse
  84. * @param dateFormats the date formats to use
  85. *
  86. * @return the parsed date
  87. *
  88. * @throws DateParseException if none of the dataFormats could parse the dateValue
  89. */
  90. public static Date parseDate(
  91. String dateValue,
  92. Collection dateFormats
  93. ) throws DateParseException {
  94. if (dateValue == null) {
  95. throw new IllegalArgumentException("dateValue is null");
  96. }
  97. if (dateFormats == null) {
  98. dateFormats = DEFAULT_PATTERNS;
  99. }
  100. // trim single quotes around date if present
  101. // see http://nagoya.apache.org/bugzilla/show_bug.cgi?id=5279
  102. if (dateValue.length() > 1
  103. && dateValue.startsWith("'")
  104. && dateValue.endsWith("'")
  105. ) {
  106. dateValue = dateValue.substring (1, dateValue.length() - 1);
  107. }
  108. SimpleDateFormat dateParser = null;
  109. Iterator formatIter = dateFormats.iterator();
  110. while (formatIter.hasNext()) {
  111. String format = (String) formatIter.next();
  112. if (dateParser == null) {
  113. dateParser = new SimpleDateFormat(format, Locale.US);
  114. dateParser.setTimeZone(TimeZone.getTimeZone("GMT"));
  115. } else {
  116. dateParser.applyPattern(format);
  117. }
  118. try {
  119. return dateParser.parse(dateValue);
  120. } catch (ParseException pe) {
  121. // ignore this exception, we will try the next format
  122. }
  123. }
  124. // we were unable to parse the date
  125. throw new DateParseException("Unable to parse the date " + dateValue);
  126. }
  127. /** This class should not be instantiated. */
  128. private DateParser() { }
  129. }