简体   繁体   中英

Regex for Java Date validation

Am checking validation of date using regex how it works for date but not for Year ? .Please help me to solve this issue.

Output :

false
true
false
true

Expected Output :

false
false
false
false


public static void main(String args[])
 {   
    System.out.println(dateFormatValidate("ddMMyyyy","^(0?[1-9]|[12][0-9]|3[01])(0?[1-9]|1[012])((19|20)\\d\\d)?", "08s21988"));
    System.out.println(dateFormatValidate("ddMMyyyy","^(0?[1-9]|[12][0-9]|3[01])(0?[1-9]|1[012])((19|20)\\d\\d)?", "08021s88"));
    System.out.println(dateFormatValidate("ddMMyyyy","^(0?[1-9]|[12][0-9]|3[01])(0?[1-9]|1[012])((19|20)\\d\\d)?", "s8021988"));
    System.out.println(dateFormatValidate("ddMMyyyy","^(0?[1-9]|[12][0-9]|3[01])(0?[1-9]|1[012])((19|20)\\d\\d)?", "0802198s"));        
 }
 public static boolean dateFormatValidate(String format, String regex, String value) {
        try {
            if (value != null && !"".equals(value.trim()) && format != null && !"".equals(format.trim())) 
            {
                if ((regex != null && !"".equals(regex.trim()) && Pattern.matches(regex, value)) || regex != null || "".equals(regex)) 
                {
                    SimpleDateFormat dformat = new SimpleDateFormat(format);
                    dformat.setLenient(false);
                    dformat.parse(value);
                    return true;
                } else
                    return false;
            } else
                return false;
        } catch (Exception e) {     
            return false;
        }
    }

@sunleo I don't think so it has anything to do with your regex, as I have just tried your pattern on these four dates you provide and it doesn't capture any of them.

I would say the error is in this if :

if ((regex != null && !"".equals(regex.trim()) && Pattern.matches(regex, value)) || regex != null || "".equals(regex)) 
{
               // your code
}

In the cases you provided in your main :

regex != null - all cases true

!"".equals(regex.trim()) - all cases true

Pattern.matches(regex, value)) - all cases false

regex != null - all cases true

"".equals(regex)) - all cases false


Which gives us following if:

if ( ( true AND true AND false ) OR true OR false )

which is the same as:

if ( false OR true OR false )

which gives in all cases:


true

So why you still managed to get two false outputs? Probably an exception was thrown here:

SimpleDateFormat dformat = new SimpleDateFormat(format);
dformat.setLenient(false);
dformat.parse(value);

So in your catch statement change return false to e.printStackTrace(); .

Also my recommendation would be to rearrange this particular if first then check it for the cases that should be false (in this example all of them). How?

First rearrange if then I would start with debugging and checking the if components to see their values and if they are computed correctly.

Also I think the regex is not correct at all (even if it's not the cause of the wrong output), if you always use ddMMyyyy format and only 19xx/20xx years, try this pattern:

^(0[1-9]|[1-2][0-9]|3[0-1])(0[1-9]|1[0-2])(19|20)\d{2}$

NOTE

I have not checked any of this (except regex) via any IDE as I do not have any here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM