Java Parse Date parseDate(final String str)

Here you can find the source of parseDate(final String str)

Description

Parses a string representing a date by trying different date patterns.

The following date patterns are tried (in the given order):

"yyyy-MM-dd", "yyyy/MM/dd", "yyyyMMdd", "yyyy", "dd.MM.yyyy", "dd MMM yyyy", "dd MMM. 

License

Apache License

Parameter

Parameter Description
str the date to parse, not null.

Exception

Parameter Description
ParseException if no pattern matches.
NullPointerException if str is null.

Return

the parsed date, or the current date if the input String (ignoring case) was "today" or "now".

Declaration

public static Date parseDate(final String str) throws ParseException 

Method Source Code

//package com.java2s;
/*//from  w  w w .j  a va  2  s . c o 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.
 */

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    private static final SimpleDateFormat DATE_PARSER = new SimpleDateFormat(
            "", Locale.ENGLISH);
    private static final ParsePosition DATE_PARSE_POSITION = new ParsePosition(
            0);
    private static final String[] DATE_PATTERNS = new String[] {
            "yyyy-MM-dd", "yyyy/MM/dd", "yyyyMMdd", "yyyy", "dd.MM.yyyy",
            "dd MMM yyyy", "dd MMM. yyyy", "MMMM yyyy", "MMM. dd, yyyy",
            "MMM. yyyy", "MMMM dd, yyyy", "MMM d, ''yy", "MMM. ''yy",
            "MMMM ''yy" };

    /**
     * <p>Parses a string representing a date by trying different date patterns.</p>
     *
     * <p>The following date patterns are tried (in the given order):</p>
     *
     * <pre>"yyyy-MM-dd", "yyyy/MM/dd", "yyyyMMdd", "yyyy", "dd.MM.yyyy", "dd MMM yyyy",
     *  "dd MMM. yyyy", "MMMM yyyy", "MMM. dd, yyyy", "MMM. yyyy", "MMMM dd, yyyy",
     *  "MMM d, ''yy", "MMM. ''yy", "MMMM ''yy"</pre>
     *
     * <p>A parse is only sucessful if it parses the whole of the input string.
     * If no parse patterns match, a ParseException is thrown.</p>
     *
     * <p>As a special case, the strings <code>"today"</code> and <code>"now"</code>
     * (ignoring case) return the current date.</p>
     *
     * @param str the date to parse, not null.
     *
     * @return the parsed date, or the current date if the input String (ignoring case) was
     *      <code>"today"</code> or <code>"now"</code>.
     *
     * @throws ParseException if no pattern matches.
     * @throws NullPointerException if str is null.
     *
     * @since 1.1.1.
     */
    public static Date parseDate(final String str) throws ParseException {
        if ("today".equalsIgnoreCase(str) || "now".equalsIgnoreCase(str)) {
            return new Date();
        }

        for (int i = 0; i < DATE_PATTERNS.length; i++) {
            DATE_PARSER.applyPattern(DATE_PATTERNS[i]);
            DATE_PARSE_POSITION.setIndex(0);
            final Date date = DATE_PARSER.parse(str, DATE_PARSE_POSITION);

            if (date != null
                    && DATE_PARSE_POSITION.getIndex() == str.length()) {
                return date;
            }
        }

        throw new ParseException("Unable to parse date: " + str, -1);
    }
}

Related

  1. parseDate(final String dateString)
  2. parseDate(final String pDate)
  3. parseDate(final String revision)
  4. parseDate(final String source)
  5. parseDate(final String source)
  6. parseDate(final String str, final Locale locale, final String... parsePatterns)
  7. parseDate(final String value)
  8. parseDate(Long date, String format)
  9. parseDate(Map obj, String field, String format)