Java Date Value Check isDate(String text, String pattern)

Here you can find the source of isDate(String text, String pattern)

Description

Validation method for date.

License

Open Source License

Parameter

Parameter Description
text Input date to be validated.
pattern Inputed date pattern.

Return

Return true if validation ok, otherwise return false.

Declaration

public static boolean isDate(String text, String pattern) 

Method Source Code


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

import java.text.SimpleDateFormat;

import java.util.Collection;

import java.util.Map;
import java.util.regex.Pattern;

public class Main {
    /**//w  w  w . j a  v a2s  .  co  m
     * Validation method for date. It is a very strict date validator.
     *
     * @param text    Input date to be validated.
     * @param pattern Inputed date pattern.
     * @return Return true if validation ok, otherwise return false.
     */
    public static boolean isDate(String text, String pattern) {
        if (text == null || text.trim().isEmpty()) {
            return false;
        }

        try {
            SimpleDateFormat formatter = new SimpleDateFormat(pattern);
            formatter.setLenient(false);
            formatter.parse(text);
        } catch (Exception e) {
            return false;
        }
        try {
            //date pattern: MM/dd/yyyy
            int mPos, dPos, yPos, mVal, dVal, yVal;
            String _pattern = pattern.toUpperCase();
            String separator = String.valueOf(getSeparator(_pattern));
            String[] _patternArr = _pattern.split(separator);
            mPos = getPosition(_patternArr, "[M]{1,}");
            dPos = getPosition(_patternArr, "[D]{1,}");
            yPos = getPosition(_patternArr, "[Y]{1,}");

            String[] dateVals = text.trim().split(separator);
            mVal = Integer.parseInt(dateVals[mPos]);
            dVal = Integer.parseInt(dateVals[dPos]);
            yVal = Integer.parseInt(dateVals[yPos]);

            if (mVal < 1 || mVal > 12) {
                return false;
            }
            if (dVal < 1 || dVal > 31) {
                return false;
            }
            if (yVal < 0 || yVal > 9999) {
                return false;
            }
            //FixBug 28507:
            //The input year's length should be the same as the year's format.
            if (dateVals[yPos].length() != _patternArr[yPos].length()) {
                return false;
            }
            int[] num_of_days_in_monthes = getNumberOfDaysInMonthes(yVal);
            if (dVal > num_of_days_in_monthes[mVal - 1]) {
                return false;
            }
        } catch (Exception eg) {
            return false;
        }
        return true;
    }

    /**
     * Check whether the Object has value or not.
     *
     * @param aObj
     * @return if the obj is empty
     */
    public static boolean isEmpty(Object aObj) {
        if (aObj instanceof String) {
            return isEmpty((String) aObj);
        } else if (aObj instanceof Long) {
            return isEmpty((Long) aObj);
        } else if (aObj instanceof java.util.Date) {
            return isEmpty((java.util.Date) aObj);
        } else if (aObj instanceof java.util.Collection) {
            return isEmpty((java.util.Collection) aObj);
        } else if (aObj instanceof java.util.Map) {
            return isEmpty((java.util.Map) aObj);
        } else if (aObj != null && aObj.getClass().isArray()) {
            return isEmptyArray(aObj);
        } else {
            return isNull(aObj);
        }
    }

    /**
     * Check whether the String has value or not.
     *
     * @param aStr
     * @return if the string is empty
     */
    public static boolean isEmpty(String aStr) {
        if (aStr == null || aStr.trim().isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Check whether the Long has value or not.
     *
     * @param aLong
     * @return if the Long is null
     */
    public static boolean isEmpty(Long aLong) {
        if (aLong == null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Check whether a Collection object is empty.
     *
     * @param c: a java.util.Collection object
     * @return if the Map is empty
     */
    public static boolean isEmpty(Collection c) {
        if (c == null || c.size() == 0) {
            return true;
        }
        return false;
    }

    /**
     * Check whether a Map object is empty.
     *
     * @param m: a java.util.Map object
     * @return if the Map is empty
     */
    public static boolean isEmpty(Map m) {
        if (m == null || m.size() == 0) {
            return true;
        }
        return false;
    }

    /**
     * Check whether the Date has value or not.
     *
     * @param aDate
     * @return if the date is null
     */
    public static boolean isEmpty(java.util.Date aDate) {
        if (aDate == null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Trim the specified String.
     *
     * @param aStr
     * @return the result string,"" return if string is NULL
     */
    public static String trim(String aStr) {
        if (aStr == null) {
            return "";
        } else {
            return aStr.trim();
        }
    }

    private static char getSeparator(String pattern) {
        for (int i = 0; i < pattern.length(); i++) {
            char c = pattern.charAt(i);
            if (c < 65 || c > 90) {
                return c;
            }
        }
        throw new java.lang.IllegalArgumentException("Inputted date pattern is fatal wrong.");
    }

    private static int getPosition(String[] patternArr, String regex) {
        for (int i = 0; i < patternArr.length; i++) {
            if (Pattern.compile(regex).matcher(patternArr[i]).matches()) {
                return i;
            }
        }
        throw new java.lang.IllegalStateException("Cannot get the position by the RegExp");
    }

    private static int[] getNumberOfDaysInMonthes(int theyear) {
        return new int[] { 31, getLeapYear(theyear), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    }

    /**
     * Check whether the array is empty or not.
     *
     * @param array
     * @return
     */
    private static boolean isEmptyArray(Object array) {
        int length = 0;
        if (array instanceof int[]) {
            length = ((int[]) array).length;
        } else if (array instanceof byte[]) {
            length = ((byte[]) array).length;
        } else if (array instanceof short[]) {
            length = ((short[]) array).length;
        } else if (array instanceof char[]) {
            length = ((char[]) array).length;
        } else if (array instanceof float[]) {
            length = ((float[]) array).length;
        } else if (array instanceof double[]) {
            length = ((double[]) array).length;
        } else if (array instanceof long[]) {
            length = ((long[]) array).length;
        } else if (array instanceof boolean[]) {
            length = ((boolean[]) array).length;
        } else {
            length = ((Object[]) array).length;
        }
        if (length == 0) {
            return true;
        }
        return false;
    }

    /**
     * Check whether the Object is null or not.
     *
     * @param oStr
     * @return if the object is NULL
     */
    public static boolean isNull(Object oStr) {
        if (oStr == null) {
            return true;
        } else {
            return false;
        }
    }

    private static int getLeapYear(int theyear) {
        return ((theyear % 4 == 0 && theyear % 100 != 0) || theyear % 400 == 0) ? 29 : 28;
    }
}

Related

  1. isDate(String str, String pattern)
  2. isDate(String strDate, SimpleDateFormat sdf)
  3. isDate(String strDate, String pattern)
  4. isDate(String strDate, String pattern)
  5. isDate(String strDate, String pattern)
  6. isDate(String val, SimpleDateFormat formatter)
  7. isDate(String value, Locale locale)
  8. isDateBefore(String date1)
  9. isDateBefore(String date1, String date2)