Java Date Value Check isValidObjectModelDate(String aDate)

Here you can find the source of isValidObjectModelDate(String aDate)

Description

is Valid Object Model Date

License

Open Source License

Parameter

Parameter Description
aDate a parameter

Declaration


public static boolean isValidObjectModelDate(String aDate) 

Method Source Code


//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    /**/*from w ww  .ja  v a2 s  .co  m*/
     *
     * @param aDate
     * @return
     *
     */
    /*==============================================================*/
    public static boolean isValidObjectModelDate(String aDate) {
        if (aDate == null) {
            return true;
        }

        if (aDate.equals("")) {
            return true;
        }

        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");

        if (aDate.length() != 10) {
            return false;
        }

        if (!(aDate.substring(4, 5).equals("-")) || !(aDate.substring(7, 8).equals("-"))) {
            return false;
        }

        try {
            Date myDate = myFormatter.parse(aDate);
        } catch (ParseException myParseException) {
            return false;
        }

        //YYYY-MM-DD
        //0123456789
        String year = aDate.substring(0, 4);
        Integer Year = new Integer(year);
        int myYear = Year.intValue();

        String month = aDate.substring(5, 7);
        Integer Month = new Integer(month);
        int myMonth = Month.intValue();

        String day = aDate.substring(8);
        Integer Day = new Integer(day);
        int myDay = Day.intValue();

        if (isValidObjectModelYear(myYear)) {
            if (isValidObjectModelMonth(myMonth)) {
                if (isValidObjectModelDay(myYear, myMonth, myDay)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     *
     * @param aYear
     * @return boolean
     *
     */
    /*==============================================================*/

    public static boolean isValidObjectModelYear(int aYear) {
        boolean validYear = false;
        if (aYear > 1800) {
            validYear = true;
        }
        return validYear;
    }

    /**
     *
     * @param aMonth
     * @return boolean
     *
     */
    /*==============================================================*/
    public static boolean isValidObjectModelMonth(int aMonth) {
        boolean result = false;
        if (aMonth > 0 && aMonth < 13) {
            result = true;
        } else {
            result = false;
        }
        return result;
    }

    /**
     *
     * @param aYear
     * @param aMonth
     * @param aDay
     *
     * @return boolean
     *
     */
    /*==============================================================*/
    public static boolean isValidObjectModelDay(int aYear, int aMonth, int aDay) {
        boolean result = false;

        result = isValidObjectModelYear(aYear);
        if (!result) {
            return result;
        }
        result = isValidObjectModelMonth(aMonth);
        if (!result) {
            return result;
        }

        //If they day is 0 or negative then it is an invalid date
        if (aDay <= 0) {
            return false;
        }

        switch (aMonth) {

        case 4:
        case 6:
        case 9:
        case 11:
            if (aDay > 30) {
                result = false;
            } else {
                result = true;
            }
            break;
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            if (aDay > 31) {
                result = false;
            } else {
                result = true;
            }
            break;
        case 2:
            GregorianCalendar gc = new GregorianCalendar();
            if (gc.isLeapYear(aYear)) {
                if (aDay > 29) {
                    result = false;
                } else {
                    result = true;
                }
            } else {
                if (aDay > 28) {
                    result = false;

                } else {
                    result = true;
                }
            }
            break;
        default:
            result = false;
        }
        return result;
    }
}

Related

  1. isValidDatePattern(String pattern)
  2. isValidDatePatterns(String dateStr, String patterns)
  3. isValidDateStr(String date, String format)
  4. isValidGameDate(String gameDate)
  5. isValidLongDateFormat(String strDate)
  6. isValidPattern(String dateString, String pattern)