Java Check Date Format Example
Chapter:
Regular Expression
Last Updated:
14-09-2016 18:42:47 UTC
Program:
/* ............... START ............... */
public class JavaCheckDateFormat {
public static void main(String[] argv) {
boolean isDate = false;
String date1 = "8-05-1988";
String date2 = "08/04/1987";
String datePattern = "\\d{1,2}-\\d{1,2}-\\d{4}";
isDate = date1.matches(datePattern);
System.out.println(
"Date :" + date1 + ": matches with the "
+ "this date " + "Pattern:" + datePattern + "Ans:" + isDate);
isDate = date2.matches(datePattern);
System.out.println(
"Date :" + date2 + ": matches with " + "the "
+ "this date Pattern:" + datePattern + "Ans:" + isDate);
}
}
/* ............... END ............... */
Output
Date :8-05-1988: matches with the this date Pattern:\d{1,2}-\d{1,2}-\d{4}Ans:true
Date :08/04/1987: matches with the this date Pattern:\d{1,2}-\d{1,2}-\d{4}Ans:false
Tags
Check Date Format Example, Java, Regular Expression