Java Date Value Check isValidDate(String psDt)

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

Description

is Valid Date

License

LGPL

Declaration

public final static boolean isValidDate(String psDt) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    private static SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

    public final static boolean isValidDate(String psDt) {
        Integer dia;/*from ww w  . j ava2 s.c om*/
        Integer mes;
        Integer ano;
        Date dataConv;
        Date dataLimiteInf;
        Date dataLimiteSup;
        String a = psDt.trim();
        if (a.length() != 10)
            return false;

        try {
            dia = new Integer(a.substring(0, 2));
            mes = new Integer(a.substring(3, 5));
            ano = new Integer(a.substring(6));
        } catch (Exception ex) {
            return false;
        }

        if (mes.intValue() > 12)
            return false;

        if (mes.intValue() == 2) {
            if (ano.intValue() % 4 == 0) {
                if (dia.intValue() > 29)
                    return false;
            } else if (dia.intValue() > 28)
                return false;
        } else if (dia.intValue() > 31)
            return false;
        else if (dia.intValue() > 30
                && (mes.intValue() == 4 || mes.intValue() == 6 || mes.intValue() == 9 || mes.intValue() == 11))
            return false;

        if (dia.intValue() > 31)
            return false;

        try {
            dataConv = formatter.parse(a);
            dataLimiteInf = formatter.parse("01/01/1900");
            dataLimiteSup = formatter.parse("06/06/2079");
        } catch (Exception e) {
            return false;
        }

        if (dataConv.after(dataLimiteSup))
            return false;

        if (dataConv.before(dataLimiteInf))
            return false;

        return true;
    }
}

Related

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