Java Parse Date parseDate(SimpleDateFormat parser, String str, String[] parsePatterns)

Here you can find the source of parseDate(SimpleDateFormat parser, String str, String[] parsePatterns)

Description

FSM - Modified the apache function to allow you to pass in a SimpleDateFormat object.

License

Apache License

Parameter

Parameter Description
str the date to parse, not null
parsePatterns the date format patterns to use, see SimpleDateFormat, not null

Exception

Parameter Description
IllegalArgumentException if the date string or pattern array is null
ParseException if none of the date patterns were suitable

Return

the parsed date

Declaration

public static Date parseDate(SimpleDateFormat parser, String str, String[] parsePatterns)
        throws ParseException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2009 Time Space Map, LLC//  ww  w . j  a v a  2  s . c om
 * 
 * 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.
 ******************************************************************************/

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**
     * <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);
    }

    /**
     * FSM - Modified the apache function to allow you to pass in a SimpleDateFormat object.
     * TODO - clean this up and send them a patch.
     *  
     * <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(SimpleDateFormat parser, String str, String[] parsePatterns)
            throws ParseException {
        if (str == null || parsePatterns == null) {
            throw new IllegalArgumentException("Date and Patterns must not be null");
        }

        ParsePosition pos = new ParsePosition(0);
        for (int i = 0; i < parsePatterns.length; i++) {
            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);
    }
}

Related

  1. parseDate(Long date, String format)
  2. parseDate(Map obj, String field, String format)
  3. parseDate(Object o)
  4. parseDate(Object str)
  5. parseDate(Object val)
  6. parseDate(String _dateString)
  7. parseDate(String actual, DateFormat format, Pattern pattern)
  8. parseDate(String applyDt)
  9. parseDate(String buildDate, SimpleDateFormat dateFormat)