Validate Date Format
This program validates date format using regex.
Problem Statement
Write a Java program to validate date in format DD/MM/YYYY.
Source Code
| 1 | public class RegexDateValidation { |
| 2 | public static void main(String[] args) { |
| 3 | System.out.println("Eduinq Regex Date Validation"); |
| 4 | String date="15/07/2026"; |
| 5 | String regex="^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/\\d{4}$"; |
| 6 | if(date.matches(regex)) System.out.println("Valid date format"); |
| 7 | else System.out.println("Invalid date format"); |
| 8 | } |
| 9 | } |
Program Output
Eduinq Regex Date Validation Valid date format
Explanation
The regex separately validates the day range (01-31), month range (01-12), and requires exactly four digits for the year, all separated by slashes, and matches() checks the whole date string against this pattern.