

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Date validation using Java Regular Expressions
The date can be validated using the java.util.regex.Pattern.matches() method. This method matches the regular expression for the date and the given input date and returns true if they match and false otherwise.
A program that demonstrates this is given as follows:
Example
public class Demo { public static void main(String args[]) { String date = "15-11-2018"; String regex = "\\d{1,2}-\\d{1,2}-\\d{4}"; System.out.println("The date is: " + date); System.out.println("Is the above date valid? " + date.matches(regex)); } }
Output
The date is: 15-11-2018 Is the above date valid? True
Now let us understand the above program.
The date is printed. The Pattern.matches() method matches the regular expression for the date and the given input date and the result is printed. A code snippet which demonstrates this is as follows:
String date = "15-11-2018"; String regex = "\\d{1,2}-\\d{1,2}-\\d{4}"; System.out.println("The date is: " + date); System.out.println("Is the above date valid? " + date.matches(regex));
- Related Questions & Answers
- Name validation using Java Regular Expressions
- Phone Number validation using Java Regular Expressions
- Zip Code validation using Java Regular Expressions
- Date format validation using Java Regex
- Java Regular Expressions Tutorial
- Java regular expressions sample examples
- Possessive quantifiers Java Regular expressions
- Java Regular expressions Logical operators
- Reluctant quantifiers Java Regular expressions
- Date format validation using C# Regex
- Greedy quantifiers Java Regular expressions in java.
- Back references in Java regular expressions
- Regular Expressions syntax in Java Regex
- Regex quantifiers in Java Regular Expressions
- Validate Phone with Java Regular Expressions
Advertisements