Skip to content

Commit ea88be3

Browse files
author
Igor Kugaudo
committed
BAEL-1351 RegEx for matching Date Pattern in Java
1 parent 5fbd24d commit ea88be3

2 files changed

Lines changed: 4 additions & 4 deletions

File tree

‎core-java/src/main/java/com/baeldung/regexp/datepattern/validator/PreciseDateValidator.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ class PreciseDateValidator implements DateValidator {
66

77
private static final Pattern DATE_PATTERN = Pattern.compile(
88
"^((2000|2400|2800|(19|2[0-9](0[48]|[2468][048]|[13579][26])))-02-29)$"
9-
+ "|^(((19|2[0-9])\\d{2})-02-(0[1-9]|1[0-9]|2[0-8]))$"
10-
+ "|^(((19|2[0-9])\\d{2})-(0[13578]|10|12)-(0[1-9]|[12][0-9]|3[01]))$"
11-
+ "|^(((19|2[0-9])\\d{2})-(0[469]|11)-(0[1-9]|[12][0-9]|30))$");
9+
+ "|^(((19|2[0-9])[0-9]{2})-02-(0[1-9]|1[0-9]|2[0-8]))$"
10+
+ "|^(((19|2[0-9])[0-9]{2})-(0[13578]|10|12)-(0[1-9]|[12][0-9]|3[01]))$"
11+
+ "|^(((19|2[0-9])[0-9]{2})-(0[469]|11)-(0[1-9]|[12][0-9]|30))$");
1212

1313
public boolean validate(String date) {
1414
return DATE_PATTERN.matcher(date).matches();

‎core-java/src/main/java/com/baeldung/regexp/datepattern/validator/RangedDateValidator.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class RangedDateValidator implements DateValidator {
66

77
private static final Pattern DATE_PATTERN = Pattern.compile(
8-
"^((19|2[0-9])\\d{2})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$");
8+
"^((19|2[0-9])[0-9]{2})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$");
99

1010
public boolean validate(String date) {
1111
return DATE_PATTERN.matcher(date).matches();

0 commit comments

Comments
 (0)