Match Date regular expression


public class MatchDates {
  public static void main(String args[]) {

    isDateValid("04-02-1995");
    isDateValid("15-42-1994");
    isDateValid("April fourth nineteen ninety nine");
    isDateValid("15-42-20001");
    isDateValid("02-02-20001");
    isDateValid("05-02-02");
    isDateValid("04-01-java2s");
  }
  public static boolean isDateValid(String date) {
    boolean retval = false;

    String datePattern = "\\d{1,2}-\\d{1,2}-\\d{4}";
    retval = date.matches(datePattern);

    String msg = "NO MATCH: pattern:" + date
        + "\r\n             regexLength: " + datePattern;

    if (retval) {
      msg = "MATCH   : pattern:" + date
          + "\r\n             regexLength: " + datePattern;
    }

    System.out.println(msg + "\r\n");
    return retval;
  }
}
Home 
  Java Book 
    Runnable examples