Java Date Value Check isValidDate(final String date, final String format, final boolean lenient)

Here you can find the source of isValidDate(final String date, final String format, final boolean lenient)

Description

Checks if the Date is valid to convert.

License

Apache License

Parameter

Parameter Description
date The Date as String
format The Format for the Date to parse
lenient Specify whether or not date/time interpretation is to be lenient.

Return

True if the Date is valid otherwise false.

Declaration

public static boolean isValidDate(final String date, final String format, final boolean lenient) 

Method Source Code

//package com.java2s;
/**//from w  w  w.  ja v a  2  s  .  co  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;

public class Main {
    /**
     * Checks if the Date is valid to convert.
     *
     * @param date
     *            The Date as String
     * @param format
     *            The Format for the Date to parse
     * @param lenient
     *            Specify whether or not date/time interpretation is to be lenient.
     * @return True if the Date is valid otherwise false.
     */
    public static boolean isValidDate(final String date, final String format, final boolean lenient) {
        boolean isValid = true;
        if (date == null || format == null || format.length() <= 0) {
            return false;
        }
        final DateFormat df = new SimpleDateFormat(format);
        df.setLenient(lenient);
        try {
            df.parse(date);
        } catch (final ParseException e) {
            isValid = false;
        }
        return isValid;
    }
}

Related

  1. isDateValid(String dateToCheck, String pattern)
  2. isDateValid(String dateToValidate)
  3. isDateValue(String inputString)
  4. isValidateData(String val)
  5. isValidDate(DateFormat sdf, String date)
  6. isValidDate(final String date, final String format, final Locale locale)
  7. isValidDate(String createDate, String startDate)
  8. isValidDate(String date)
  9. isValidDate(String date)