Java Date Value Check isValidDate(String inDate)

Here you can find the source of isValidDate(String inDate)

Description

is Valid Date

License

Open Source License

Declaration

public static boolean isValidDate(String inDate) 

Method Source Code


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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Main {
    private static List<SimpleDateFormat> dateFormats = new ArrayList<SimpleDateFormat>() {
        {//from   w  ww  .j  a  v a2 s  .c  o m
            add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            add(new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"));
            add(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"));
            add(new SimpleDateFormat("yyyy-MM-dd"));
            add(new SimpleDateFormat("yyyy.MM.dd"));
            add(new SimpleDateFormat("yyyy/MM/dd"));

        }
    };

    public static boolean isValidDate(String inDate) {
        Date date = null;
        for (SimpleDateFormat format : dateFormats) {
            format.setLenient(false);
            try {
                if (inDate.length() < 12 && format.toPattern().length() < 12) {
                    date = format.parse(inDate.trim());
                } else if (inDate.length() > 12 && format.toPattern().length() > 12) {
                    date = format.parse(inDate.trim());
                }

            } catch (ParseException e) {
                continue;
            }
            if (date != null) {
                break;
            }
        }
        if (date == null) {
            return false;
        }
        return true;
    }
}

Related

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