
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to validate given date formats like MM-DD-YYYY using regex in java?
The java.util.regex package of java provides various classes to find particular patterns in character sequences.
The pattern class of this package is a compiled representation of a regular expression. To match a regular expression with a String this class provides two methods namely −
compile() − This method accepts a string representing a regular expression and returns an object of the Pattern object.
matcher() − This method accepts a String value and creates a matcher object which matches the given String to the pattern represented by the current pattern object.
Following is the regular expression to match date in dd-MM-yyyy format:
^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$
Therefore, to validate a date String of the format MM-DD-YYYY −
Compile the above mentioned regular expression using the compile() method of the Pattern class and retrieve the Pattern object.
Using the object obtained above, invoke the matcher() method by passing the required date string as a parameter and retrieve the Matcher object from this method.
The matches() of the Matcher class returns true in case of a match else, it returns false. Invoker this method on the matcher object obtained from the previous step.
Example
import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchingDate { public static void main(String[] args) { String date = "01/12/2019"; String regex = "^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(date); boolean bool = matcher.matches(); if(bool) { System.out.println("Date is valid"); } else { System.out.println("Date is not valid"); } } }
Output
Date is valid
- Related Articles
- Accepting date strings (MM-dd-yyyy format) using Java regex?
- How to parse Date from String in the format: dd/MM/yyyy to dd/MM/yyyy in java?
- How to format a string to date in as dd-MM-yyyy using java?
- MySQL query to convert YYYY-MM-DD to DD Month, YYYY date format
- Java Program to format date in mm-dd-yyyy hh:mm:ss format
- Program to reformat date in YYYY-MM-DD format using Python
- Convert MySQL date format from yyyy-mm-ddThh:mm:ss.sssZ to yyyy-mm-dd hh:mm:ss ?
- How to Convert YYYY-MM-DD to Standard Date in Excel?
- How to format JavaScript date into yyyy-mm-dd format?
- MySQL date format DD/MM/YYYY select query?
- How to convert Python date string mm/dd/yyyy to datetime?
- MySQL date format to convert dd.mm.yy to YYYY-MM-DD?
- How to convert MM/YY to YYYY-MM-DD in MYSQL?
- Get date format DD/MM/YYYY with MySQL Select Query?
- Get today's date in (YYYY-MM-DD) format in MySQL?
