Java Date Value Check isValidDate(String dateString, String dateFormat)

Here you can find the source of isValidDate(String dateString, String dateFormat)

Description

Checks whether the actual date of the given date string is valid based on the given date format pattern.

License

Apache License

Parameter

Parameter Description
dateString The date string.
dateFormat The date format pattern which should respect the SimpleDateFormat rules.

Return

True if the actual date of the given date string is valid based on the given date format pattern.

Declaration

public static boolean isValidDate(String dateString, String dateFormat) 

Method Source Code

//package com.java2s;
/**/* w  ww  .  ja v  a 2  s .  com*/
 * Copyright (C) 2016 Hurence (support@hurence.com)
 *
 * 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 org.joda.time.DateTime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    private static final Map<String, String> DATE_FORMAT_REGEXPS = new HashMap<String, String>() {
        {
            put("^[A-Z,a-z]{3}\\s[A-Z,a-z]{3}\\s\\d{1,2}\\s\\d{1,2}:\\d{1,2}:\\d{1,2}\\s[A-Z,a-z]{3}\\s\\d{4}$",
                    "EEE MMM dd hh:mm:ss zzz yyyy");
            put("^[A-Z,a-z]{3},\\s\\d{1,2}\\s[A-Z,a-z]{3}\\s\\d{4}\\s\\d{1,2}:\\d{1,2}:\\d{1,2}\\s[A-Z,a-z]{3}$",
                    "EEE, dd MMM yyyy HH:mm:ss z");
            put("^\\d{4}-\\d{2}-\\d{2}[T,t]\\d{2}:\\d{2}:\\d{2}\\.\\d{3}[A-Z,a-z]{3}$",
                    "yyyy-MM-dd'T'HH:mm:ss.SSSZ");
            put("^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2},\\d{3}$", "yyyy-MM-dd HH:mm:ss,SSS");
            put("^\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}\\.\\d{3}$", "yyyy-MM-dd HH:mm:ss.SSS");
            put("^\\d{4}-\\d{2}-\\d{2}[T,t]\\d{2}:\\d{2}:\\d{2}[A-Z,a-z]{3}$", "yyyy-MM-dd'T'HH:mm:ssz");
            put("^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyy-MM-dd HH:mm:ss");
            put("^\\d{4}-\\d{2}-\\d{2}[T,t]\\d{2}:\\d{2}:\\d{2}\\.\\d{4}\\+\\d{2}:\\d{2}$",
                    "yyyy-MM-dd'T'HH:mm:ss.SSSSX");
            put("^\\d{8}$", "yyyyMMdd");
            put("^\\d{1,2}-\\d{1,2}-\\d{4}$", "dd-MM-yyyy");
            put("^\\d{4}-\\d{1,2}-\\d{1,2}$", "yyyy-MM-dd");
            put("^\\d{1,2}/\\d{1,2}/\\d{4}$", "MM/dd/yyyy");
            put("^\\d{4}/\\d{1,2}/\\d{1,2}$", "yyyy/MM/dd");
            put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}$", "dd MMM yyyy");
            put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}$", "dd MMMM yyyy");
            put("^\\d{12}$", "yyyyMMddHHmm");
            put("^\\d{8}\\s\\d{4}$", "yyyyMMdd HHmm");
            put("^\\d{1,2}-\\d{1,2}-\\d{4}\\s\\d{1,2}:\\d{2}$", "dd-MM-yyyy HH:mm");
            put("^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{1,2}:\\d{2}$", "yyyy-MM-dd HH:mm");
            put("^\\d{1,2}/\\d{1,2}/\\d{4}\\s\\d{1,2}:\\d{2}$", "dd/MM/yyyy HH:mm");
            put("^\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{2}$", "yyyy/MM/dd HH:mm");
            put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}\\s\\d{1,2}:\\d{2}$", "dd MMM yyyy HH:mm");
            put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}$", "dd MMMM yyyy HH:mm");
            put("^\\d{14}$", "yyyyMMddHHmmss");
            put("^\\d{8}\\s\\d{6}$", "yyyyMMdd HHmmss");
            put("^\\d{1,2}-\\d{1,2}-\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd-MM-yyyy HH:mm:ss");

            put("^\\d{1,2}/\\d{1,2}/\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd/MM/yyyy HH:mm:ss");
            put("^\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "yyyy/MM/dd HH:mm:ss");
            put("^\\d{1,2}\\s[a-z]{3}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd MMM yyyy HH:mm:ss");
            put("^\\d{1,2}\\s[a-z]{4,}\\s\\d{4}\\s\\d{1,2}:\\d{2}:\\d{2}$", "dd MMMM yyyy HH:mm:ss");

            put("^\\d{1,2}/\\w{3}/\\d{4}:\\d{2}:\\d{2}:\\d{2}\\s[-\\+]\\d{4}$", "dd/MMM/yyyy:HH:mm:ss Z"); //"02/JAN/2014:09:43:49 +0200"
            put("^\\w{3}\\s\\d{1,2}\\s\\d{1,2}:\\d{2}:\\d{2}$", "MMM dd HH:mm:ss"); // "Jan 02 07:43:49"
            put("^\\d{2}/\\d{2}/\\d{4}-\\d{2}:\\d{2}:\\d{2}:\\d{3}$", "dd/MM/yyyy-HH:mm:ss:SSS"); //'02/01/2016-07:43:49,000'
            put("^\\d{1,2}-\\w{3}-\\d{4}\\s\\d{2}:\\d{2}:\\d{2}:\\d{3}$", "dd-MMM-yyyy HH:mm:ss:SSS");
            put("^\\d{1,2}-\\w{3}-\\d{4}\\s\\d{2}:\\d{2}:\\d{2}.\\d{3}$", "dd-MMM-yyyy HH:mm:ss.SSS");
            put("^\\d{4}\\s+\\w{3}\\s+\\d{1,2}\\s+\\d{1,2}:\\d{1,2}:\\d{1,2}$", "yyyy MMM dd HH:mm:ss"); // '2017 Jan 17 18:52:18'
            put("^\\d{4}$", "yyyy"); // '2017'
        }
    };

    /**
     * Checks whether the actual date of the given date string is valid. This makes use of the
     * {@link DateUtil#determineDateFormat(String)} to determine the SimpleDateFormat pattern to be
     * used for parsing.
     * @param dateString The date string.
     * @return True if the actual date of the given date string is valid.
     */
    public static boolean isValidDate(String dateString) {
        try {
            parse(dateString);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * Checks whether the actual date of the given date string is valid based on the given date
     * format pattern.
     * @param dateString The date string.
     * @param dateFormat The date format pattern which should respect the SimpleDateFormat rules.
     * @return True if the actual date of the given date string is valid based on the given date
     * format pattern.
     * @see SimpleDateFormat
     */
    public static boolean isValidDate(String dateString, String dateFormat) {
        try {
            parse(dateString, dateFormat);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * Parse the given date string to date object and return a date instance based on the given
     * date string. This makes use of the {@link DateUtil#determineDateFormat(String)} to determine
     * the SimpleDateFormat pattern to be used for parsing.
     * @param dateString The date string to be parsed to date object.
     * @return The parsed date object.
     * @throws ParseException If the date format pattern of the given date string is unknown, or if
     * the given date string or its actual date is invalid based on the date format pattern.
     */
    public static Date parse(String dateString) throws ParseException {
        return parse(dateString, TimeZone.getTimeZone("UTC"));
    }

    /**
     * Parse the given date string to date object and return a date instance based on the given
     * date string. This makes use of the {@link DateUtil#determineDateFormat(String)} to determine
     * the SimpleDateFormat pattern to be used for parsing.
     * @param dateString The date string to be parsed to date object.
     * @param timezone The timezone to use to parse the date.
     * @return The parsed date object.
     * @throws ParseException If the date format pattern of the given date string is unknown, or if
     * the given date string or its actual date is invalid based on the date format pattern.
     */
    public static Date parse(String dateString, TimeZone timezone) throws ParseException {
        String dateFormat = determineDateFormat(dateString);
        if (dateFormat == null) {
            throw new ParseException("Unknown date format for date \'" + dateString + "\'", 0);
        }
        return parse(dateString, dateFormat, timezone);
    }

    /**
     * Validate the actual date of the given date string based on the given date format pattern and
     * return a date instance based on the given date string using UTC timezone.
     * @param dateString The date string.
     * @param dateFormat The date format pattern which should respect the SimpleDateFormat rules.
     * @return The parsed date object.
     * @throws ParseException If the given date string or its actual date is invalid based on the
     * given date format pattern.
     * @see SimpleDateFormat
     */
    public static Date parse(String dateString, String dateFormat) throws ParseException {
        return parse(dateString, dateFormat, TimeZone.getTimeZone("UTC"));
    }

    /**
     * Validate the actual date of the given date string based on the given date format pattern and
     * return a date instance based on the given date string.
     * @param dateString The date string.
     * @param dateFormat The date format pattern which should respect the SimpleDateFormat rules.
     * @param timeZone The timezone to use to parse the date.
     * @return The parsed date object.
     * @throws ParseException If the given date string or its actual date is invalid based on the
     * given date format pattern.
     * @see SimpleDateFormat
     */
    public static Date parse(String dateString, String dateFormat, TimeZone timeZone) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat, new Locale("en", "US"));
        simpleDateFormat.setTimeZone(timeZone);
        simpleDateFormat.setLenient(false); // Don't automatically convert invalid date.
        if (dateFormat.equals("MMM dd HH:mm:ss")) {

            DateTime today = new DateTime();
            DateTime parsedDate = new DateTime(simpleDateFormat.parse(dateString));

            DateTime parsedDateWithTodaysYear = parsedDate.withYear(today.getYear());

            return parsedDateWithTodaysYear.toDate();
        } else {
            return simpleDateFormat.parse(dateString);
        }
    }

    /**
     * Determine SimpleDateFormat pattern matching with the given date string. Returns null if
     * format is unknown. You can simply extend DateUtil with more formats if needed.
     * @param dateString The date string to determine the SimpleDateFormat pattern for.
     * @return The matching SimpleDateFormat pattern, or null if format is unknown.
     * @see SimpleDateFormat
     */
    public static String determineDateFormat(String dateString) {
        for (String regexp : DATE_FORMAT_REGEXPS.keySet()) {
            if (dateString.toLowerCase().matches(regexp)) {
                return DATE_FORMAT_REGEXPS.get(regexp);
            }
        }
        return null; // Unknown format.
    }
}

Related

  1. isValidDate(String dateString)
  2. isValidDate(String dateString)
  3. isValidDate(String dateString, String dateFormat)
  4. isValidDate(String dateString, String dateFormat)
  5. isValidDate(String dateString, String dateFormat)
  6. isValidDate(String dateString, String dateFormatPattern)
  7. isValidDate(String dateString, String format)
  8. isValidDate(String dt)
  9. isValidDate(String inDate)