Java Date Value Check isValidDatePatterns(String dateStr, String patterns)

Here you can find the source of isValidDatePatterns(String dateStr, String patterns)

Description

is Valid Date Patterns

License

Open Source License

Declaration

public static boolean isValidDatePatterns(String dateStr, String patterns) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    private static String defaultPattern = "yyyy-MM-dd";

    public static boolean isValidDatePatterns(String dateStr, String patterns) {
        if (patterns == null || "".equalsIgnoreCase(patterns) || patterns.length() < 1) {
            patterns = "yyyy-MM-dd;dd/MM/yyyy;yyyy/MM/dd;yyyy/M/d h:mm";
        }//from   w w  w .  j  av  a  2s  .  c o  m
        boolean isValid = false;
        String[] patternArr = patterns.split(";");
        for (int i = 0; i < patternArr.length; i++) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(patternArr[i]);
                String date = sdf.format(sdf.parse(dateStr));
                if (date.equalsIgnoreCase(dateStr)) {
                    isValid = true;
                    defaultPattern = patternArr[i];
                    break;
                }
            } catch (Exception e) {
                isValid = false;
            }
        }
        return isValid;
    }

    public static Date parse(String source, String format) throws ParseException {
        if (source == null) {
            return null;
        }

        DateFormat df = null;
        if (format != null) {
            df = new SimpleDateFormat(format);
        } else {
            df = DateFormat.getDateInstance(DateFormat.DEFAULT);
        }

        return df.parse(source);

    }
}

Related

  1. isValidDate(String value)
  2. isValidDateFormat(String startDate)
  3. isValidDateFormat(String strDate, String dataFormat)
  4. isValidDateInternal(String date, String dateFormat)
  5. isValidDatePattern(String pattern)
  6. isValidDateStr(String date, String format)
  7. isValidGameDate(String gameDate)
  8. isValidLongDateFormat(String strDate)
  9. isValidObjectModelDate(String aDate)