In this example we will show how to validate date format using regular expression in Java. Here you can validate dates in this regular: “((0?[1-9]) | ([012] 1))/((0?[1-9]) | | ([12] d) (3 [01]))/(20) 19 | d {2}”.
Source Code
package com.beginner.examples;
public class VerifyDateExample {
public static void main(String[] args) {
//
String reg = "((0?[1-9])|(1[012]))/((0?[1-9])|([12]\d)|(3[01]))/(19|20)\d{2}";
String dateStr1 = "2/02/2019";
String dateStr2 = "13/20/2018";
System.out.println(dateStr1+" : "+ dateStr1.matches(reg));
System.out.println(dateStr2+" : "+ dateStr2.matches(reg));
}
}
Output:
2/02/2019 : true
13/20/2018 : false