Java Date Value Check isDate(final String strDate, final String pattern)

Here you can find the source of isDate(final String strDate, final String pattern)

Description

Checks if is date.

License

Apache License

Parameter

Parameter Description
strDate the str date
pattern the pattern

Return

true, if is date

Declaration

public static boolean isDate(final String strDate, final String pattern) 

Method Source Code

//package com.java2s;
/*/*w w  w .jav a  2s .  com*/
 * Copyright 2002-2016 Jalal Kiswani.
 *
 * 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.Locale;

public class Main {
    /**
     * Checks if is date.
     *
     * @param strDate
     *            the str date
     * @param pattern
     *            the pattern
     * @return true, if is date
     */
    public static boolean isDate(final String strDate, final String pattern) {
        try {
            parseDate(strDate, pattern);
            return true;
        } catch (final Exception e) {
            return false;
        }
    }

    /**
     * Parses the date.
     *
     * @param strDate
     *            the str date
     * @param pattern
     *            the pattern
     * @return the java.util. date
     * @throws ParseException
     *             the parse exception
     */
    public static java.util.Date parseDate(final String strDate, final String pattern) throws ParseException {
        final SimpleDateFormat parser = new SimpleDateFormat(pattern, Locale.US);
        final Date date = parser.parse(strDate);
        return date;
    }
}

Related

  1. isData(long date)
  2. isDataOra(String date)
  3. isDate(final String date)
  4. isDate(final String date)
  5. isDate(final String dateString)
  6. isDate(Object object)
  7. isDate(String date)
  8. isDate(String dateStr, String dateFormat)
  9. isDate(String dateString)