Java Parse Date parseDate(final String date, final List patterns)

Here you can find the source of parseDate(final String date, final List patterns)

Description

Tries to convert the given String to a Date.

License

Apache License

Parameter

Parameter Description
date The date to convert as String.
patterns The date patterns to convert the String to a date-object.

Return

Gives a Date if the convertion was successfull otherwise false.

Declaration

public static Date parseDate(final String date, final List<String> patterns) 

Method Source Code

//package com.java2s;
/**//from  www. j ava 2s .  com
 * Copyright (C) 2007 Asterios Raptis
 *
 * 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.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class Main {
    /**
     * Tries to convert the given String to a Date.
     *
     * @param date
     *            The date to convert as String.
     * @param patterns
     *            The date patterns to convert the String to a date-object.
     * @return Gives a Date if the convertion was successfull otherwise false.
     */
    public static Date parseDate(final String date, final List<String> patterns) {
        for (String pattern : patterns) {
            final SimpleDateFormat formatter = new SimpleDateFormat(pattern);
            try {
                final Date result = formatter.parse(date);
                return result;
            } catch (final ParseException e) {
                // Do nothing...
            }
        }
        return null;
    }
}

Related

  1. parseDate(final Date date)
  2. parseDate(final String date)
  3. parseDate(final String date)
  4. parseDate(final String date)
  5. parseDate(final String date)
  6. parseDate(final String date, final String format)
  7. parseDate(final String dateStr)
  8. parseDate(final String dateString)
  9. parseDate(final String pDate)