Description
Parses the date value using the given date formats.
License
Apache License
Parameter
Parameter | Description |
---|
dateValue | the date value to parse |
dateFormats | the date formats to use |
startDate | During parsing, two digit years will be placed in the range <code>startDate</code> to <code>startDate + 100 years</code>. This value may be <code>null</code>. When <code>null</code> is given as a parameter, year <code>2000</code> will be used. |
Exception
Parameter | Description |
---|
DateParseException | if none of the dataFormats could parse the dateValue |
Return
the parsed date
Declaration
public static Date parseDate(String dateValue, String[] dateFormats,
Date startDate) throws DateParseException
Method Source Code
/*/* ww w. j a v a2 s .co m*/
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
* <http://www.apache.org/>.
*
*/
import java.lang.ref.SoftReference;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import org.apache.ogt.http.annotation.Immutable;
public class Main{
private static final String[] DEFAULT_PATTERNS = new String[] {
PATTERN_RFC1036, PATTERN_RFC1123, PATTERN_ASCTIME };
private static final Date DEFAULT_TWO_DIGIT_YEAR_START;
/**
* Parses a date value. The formats used for parsing the date value are retrieved from
* the default http params.
*
* @param dateValue the date value to parse
*
* @return the parsed date
*
* @throws DateParseException if the value could not be parsed using any of the
* supported date formats
*/
public static Date parseDate(String dateValue)
throws DateParseException {
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(final String dateValue,
String[] dateFormats) throws DateParseException {
return parseDate(dateValue, dateFormats, null);
}
/**
* Parses the date value using the given date formats.
*
* @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
* <code>startDate</code> to <code>startDate + 100 years</code>. This value may
* be <code>null</code>. When <code>null</code> is given as a parameter, year
* <code>2000</code> will be used.
*
* @return the parsed date
*
* @throws DateParseException if none of the dataFormats could parse the dateValue
*/
public static Date parseDate(String dateValue, String[] dateFormats,
Date startDate) throws DateParseException {
if (dateValue == null) {
throw new IllegalArgumentException("dateValue is null");
}
if (dateFormats == null) {
dateFormats = DEFAULT_PATTERNS;
}
if (startDate == null) {
startDate = DEFAULT_TWO_DIGIT_YEAR_START;
}
// trim single quotes around date if present
// see issue #5279
if (dateValue.length() > 1 && dateValue.startsWith("'")
&& dateValue.endsWith("'")) {
dateValue = dateValue.substring(1, dateValue.length() - 1);
}
for (String dateFormat : dateFormats) {
SimpleDateFormat dateParser = DateFormatHolder
.formatFor(dateFormat);
dateParser.set2DigitYearStart(startDate);
try {
return dateParser.parse(dateValue);
} catch (ParseException pe) {
// ignore this exception, we will try the next format
}
}
// we were unable to parse the date
throw new DateParseException("Unable to parse the date "
+ dateValue);
}
}
Related
- parseDate(String date, String pattern)
- parseDate(String date, String pattern)
- parseDate(String date, String pattern, TimeZone zone)
- parseDate(String dateValue)
- parseDate(String dateValue)
- parseDate(String dateValue, String[] dateFormats, Date startDate)
- parseDate(String str)
- parseDate(String str, String parsePattern)
- parseDate(String str, String... parsePatterns)