Java Date Validation Using RegEx - HowToDoInJava
Java Date Validation Using RegEx - HowToDoInJava
Java Regex Home / Java / Java Regular Expressions / Java date validation using Search Tutorials
Tutorial RegEx
Type and Press ENTE
Java Regex –
Java date validation using
RegEx
Introduction
Java Regex :
Alphanumeric ADVERTISEMENTS
Java Regex : Credit In this Java date validation using regex, we will learn to
Card Numbers validate simple date formats such as mm/dd/yy, mm/dd/yyyy,
Java Regex : Canadian dd/mm/yy and dd/mm/yyyy. Here we want to use a regex that
Postcodes
simply checks whether the input is a valid date without trying
Java Regex : Currency
to eliminate things such as February 31st.
Symbol
Java Regex : Date We might think that something as conceptually trivial as a date
Format
validation should be an easy job for a regular expression. But it
Java Regex : Email
isn’t. The main issue is that regular expressions don’t deal
Address
directly with numbers.
Java Regex : Password
Java Regex : Greek ADVERTISEMENTS
ADVERTISEMENTS
Characters
Java Regex : ISBNs
Java Regex : Min/Max
Length
Java Regex : No. of
Lines
Java Regex : No. of
Words
Java Regex : SSN
Java Regex : UK Postal
Codes
Java Regex : US Postal We can’t tell a regular expression to “match a number between
ADVERTISEMENTS
Codes 1 and 31”. Rather regular expressions work character by
Java Regex : Trademark character.
Symbol
Java Regex : Intl Phone We use '3[01]|[12][0-9]|0?[1-9]' to match 3 followed by
Numbers 0 or 1, or to match 1 or 2 followed by any digit, or to match an
North American Phone optional 0 followed by 1 to 9. Because of this, you have to
Numbers
choose how simple or how accurate you want your regular
expression to be.
Java Tutorial
Java Introduction
1. Java date validation regex – allow
Java Keywords
Java Flow Control
leading zeros to be omitted ADVERTISEMENTS
Java OOP
Java Inner Class Regex : ^[0-3]?[0-9]/[0-3]?[0-9]/(?:[0-9]{2})?[0-9]{2}$
Java String
https://howtodoinjava.com/java/regex/java-regex-date-format-validation/ 1/8
8/7/2020 Java date validation using RegEx - HowToDoInJava
Java Enum
List dates = new ArrayList();
Java Collections
dates.add("1/1/11");
Java ArrayList
dates.add("01/01/11");
Java HashMap dates.add("01/01/2011");
Java Array dates.add("01/1/2011");
Java Sort dates.add("1/11/2011");
Java Clone dates.add("1/11/11");
Java Date Time
dates.add("11/1/11");
Java Concurrency
String regex = "^[0-3]?[0-9]/[0-3]?[0-9]/(?:[0-9]{2})?[
Java Generics
Java Serialization Pattern pattern = Pattern.compile(regex);
Java Input Output
Java New I/O for(String date : dates)
Java Exceptions {
Java Annotations
Matcher matcher = pattern.matcher(date);
System.out.println(date +" : "+ matcher.matches());
Java Reflection
}
Java Garbage collection
Java JDBC
Java Security
Program output.
Java Regex
Java Servlets ADVERTISEMENTS
Java XML
Java Puzzles ADVERTISEMENTS
Java Examples
Java Libraries
Java Resources
Java 14
Java 12
Java 11
Java 10
Java 9
Java 8
Java 7 Console
1/1/11 : true
01/01/11 : true
01/01/2011 : true
01/1/2011 : true
1/11/2011 : true
1/11/11 : true
11/1/11 : true
Regex : ^[0-3][0-9]/[0-3][0-9]/(?:[0-9][0-9])?[0-9][0-9]$
https://howtodoinjava.com/java/regex/java-regex-date-format-validation/ 2/8
8/7/2020 Java date validation using RegEx - HowToDoInJava
Program output.
Console
01/01/11 : true
01/01/2011 : true
1/1/11 : false
01/1/2011 : false
1/11/2011 : false
1/11/11 : false
11/1/11 : false
Regex : ^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$
https://howtodoinjava.com/java/regex/java-regex-date-format-validation/ 3/8
8/7/2020 Java date validation using RegEx - HowToDoInJava
dates.add("01/01/2011");
Program output.
Console
01/01/11 : false
01/01/2011 : true
1/1/11 : false
01/1/2011 : false
Regex : ^(3[01]|[12][0-9]|0[1-9])/(1[0-2]|0[1-9])/[0-9]{4}$
https://howtodoinjava.com/java/regex/java-regex-date-format-validation/ 4/8
8/7/2020 Java date validation using RegEx - HowToDoInJava
Program output.
Console
07/13/2011 : false
13/07/2011 : true
1/1/11 : false
01/1/2011 : false
Feel free to use and edit above regular expressions for date
validation to suit your needs.
Happy Learning !!
ADVERTISEMENTS
ADVERTISEMENTS
https://howtodoinjava.com/java/regex/java-regex-date-format-validation/ 5/8
8/7/2020 Java date validation using RegEx - HowToDoInJava
Amit Kushawaha
Reply
Lokesh Gupta
try
{
LocalDate.parse("2016-02-30",
formatter.withResolverStyle(Res
}
catch (DateTimeException ex)
{
System.out.println(ex.getMessage());
}
Reply
mofis khan
June 6, 2016
Reply
Priyanka