Skip to content
Related Articles
Open in App
Not now

Related Articles

Java | Date format validation using Regex

Improve Article
Save Article
  • Last Updated : 30 Oct, 2018
Improve Article
Save Article

We use java.util.regex.Pattern.compile(String regex) method which compiles the given regular expression into a pattern. Here regex is the expression to be compiled.

// Java program to check if given date is
// valid or not.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GeeksforGeeks {
    // Returns true if d is in format
    // /dd/mm/yyyy
    public static boolean isValidDate(String d)
        String regex = "^(1[0-2]|0[1-9])/(3[01]"
                       + "|[12][0-9]|0[1-9])/[0-9]{4}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher((CharSequence)d);
        return matcher.matches();
    public static void main(String args[])



More ways to validate a date :

  1. SimpleDateFormat class. We can use parse method of this class to validate the date.
  2. Writing our own method to check if a date is valid.
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!