Date Validator Java

Below demo program shows how to validate date java. Date validator java class you could use different

pattern to validate date. Below two pattern has been used to validate date.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateValidator {

 private static Pattern pattern;
 private static Matcher matcher;

 // pattern dd/mm/yyyy
 private static final String DATE_PATTERN_DD_MM_YYYY = "(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)";
 // pattern mm/dd/yyyy
 private static final String DATE_PATTERN_MM_DD_YYYY = "(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])/((19|20)\\d\\d)";

 public static void main(String args[]){

        System.out.println(validate("05/12/2013"));
    }
 public static boolean validate(final String value) {
  pattern = Pattern.compile(DATE_PATTERN_MM_DD_YYYY);
  matcher = pattern.matcher(value);
  return matcher.matches();
 }

}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *