Java Parse Date Pattern YYYY parseToDate(final String date, final String format)

Here you can find the source of parseToDate(final String date, final String format)

Description

Parses the String date to a date object.

License

Apache License

Parameter

Parameter Description
date The Date as String
format The Format for the Date to parse

Exception

Parameter Description
ParseException occurs when their are problems with parsing the String to Date.

Return

The formated Date

Declaration

public static Date parseToDate(final String date, final String format) throws ParseException 

Method Source Code

//package com.java2s;
/**/*w w  w  .  j a va  2  s.c o m*/
 * 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.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.Locale;

public class Main {
    /**
     * Parses the String date to a date object. For example: USA-Format is : yyyy-MM-dd
     *
     * @param date
     *            The Date as String
     * @param format
     *            The Format for the Date to parse
     * @return The formated Date
     * @throws ParseException
     *             occurs when their are problems with parsing the String to Date.
     */
    public static Date parseToDate(final String date, final String format) throws ParseException {
        final DateFormat df = new SimpleDateFormat(format);
        Date parsedDate = null;
        parsedDate = df.parse(date);
        return parsedDate;
    }

    /**
     * Returns a date-object if the array with the formats are valid otherwise null.
     *
     * @param datum
     *            The date as string which to parse to a date-object.
     * @param formats
     *            The string-array with the date-patterns.
     * @param locale
     *            THe Locale for the SimpleDateFormat.
     * @return A date-object if the array with the formats are valid otherwise null.
     */
    public static Date parseToDate(final String datum, final String[] formats, final Locale locale) {
        for (String format : formats) {
            final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
            try {
                return sdf.parse(datum);
            } catch (final ParseException e) {
                // Do nothing...
            }
        }
        return null;
    }
}

Related

  1. parseStringToDate(String date)
  2. parseStringToDate(String dateString)
  3. parseStringToDatePrettyPrint(Date date)
  4. parseStringToUtilDate(String strDate)
  5. parseTIPPDate(String dateString)
  6. parseToDate(String date)
  7. parseToDate(String date, String format)
  8. parseToDateLenient(final String date, final String format, final boolean lenient)
  9. parseToGregorianCalendar(String dateString)