How to Validate a Date in String Format Using Java Regex.


1. Introduction

In this article, you'll learn how to validate a date in text format using Java regular expression in the yyyy-MM-dd format.

2. Regular Expressions

Regular expressions, or regex, help test a string against a pattern without too much trouble. In short, a regex creates a pattern that tests a text given as input.

Explaining the regular expression used in this post can become too much complicated. This post will focus on how to use it in our Java programs. Check out a complete article about Regular Expressions in Java.

We'll use a specific pattern for this post to test our dates. Check out this website for more details about the regular expression used.

3. Validate a Date Using the String.matches Method

You can use the matches method from the String class to validate a date. The method receives a regular expression and returns a boolean with the test result. Let's try one example:

1  public static void main(String[] args) {
2    String date1 = "2022-09-06";
3    String date2 = "202-09-06";
5    boolean valid1 = date1.matches("^\\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$");
6    boolean valid2 = date2.matches("^\\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$");
7    System.out.println(valid1); // -> true
8    System.out.println(valid2); // -> false
9  }

In the above example, the date2 is invalid because it is missing one number in the year. The date1 is valid since it contains all the correct numbers for a yyyy-MM-dd date.

4. Validate a Date Using the Pattern.matches Method

You can have the same result as the String.matches method by using the Pattern.matches method. The Pattern.matches receives the regex pattern and the test String. Let's see how it works:

 1  public static void main(String[] args) {
 2    String date1 = "2022-09-06";
 3    String date2 = "202-09-06";
 5    //Method 2 - using Pattern.matches() method
 6    boolean valid3 = Pattern.matches("^\\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$", date1);
 7    boolean valid4 = Pattern.matches("^\\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$", date2);
 8    System.out.println(valid3); // -> true
 9    System.out.println(valid4); // -> false
10  }

Using Pattern.matches, we achieve the same result as the String.matches method.

5. Validate a Date Using the Pattern.compile and Matcher.matches Methods

There's another way to achieve the same result using the Pattern and Matcher classes:

  1. The Pattern.compile method receives a regex and returns a Pattern object.
  2. Then, we call the matcher method from that Pattern object, passing the test string as the argument.
  3. Finally, we call the matches method from our Matcher object without arguments to test the input String.

Let's try one example in code:

 1  public static void main(String[] args) {
 2    String date1 = "2022-09-06";
 3    String date2 = "202-09-06";
 5    //Method 3 - using Pattern.compile() and Matcher.matches() methods
 6    Pattern pattern = Pattern.compile("^\\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$");
 7    boolean valid5 = pattern.matcher(date1).matches();
 8    boolean valid6 = pattern.matcher(date2).matches();
 9    System.out.println(valid5); // -> true
10    System.out.println(valid6); // -> false
11  }

Important note: All the methods described in this article throw a PatternSyntaxException if the regex provided is invalid. Experimenting with your regular expressions using online testers like is essential.

6. Conclusion

This article shows how to validate a date in text format using Java regular expressions.