Date Format Validation with java

View: 255    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Javascript   Fields: Other

10 point/1 review File has been tested

In this regex tutorial, we will learn to validate simple date formats e.g. mm/dd/yy, mm/dd/yyyy, dd/mm/yy, and dd/mm/yyyy. Here, we want to use a regex that simply checks whether the input looks like a date, without trying to eliminate things such as February 31st.

Introduction

In this regex tutorial, we will learn to validate simple date formats e.g. mm/dd/yy, mm/dd/yyyy, dd/mm/yy, and dd/mm/yyyy. Here, we want to use a regex that simply checks whether the input looks like a date, without trying to eliminate things such as February 31st.

You might think that something as conceptually trivial as a date should be an easy job for a regular expression. But it isn’t. The main issue is that regular expressions don’t deal directly with numbers. You can’t tell a regular expression to “match a number between 1 and 31”. Rather regular expressions work character by character.

We use ‹3[01]|[12][0-9]|0?[1-9]› to match 3 followed by 0 or 1, or to match 1 or 2 followed by any digit, or to match an optional 0 followed by 1 to 9. Because of this, you have to choose how simple or how accurate you want your regular expression to be.

Let’s see some regex examples based on different requirements around date formats mm/dd/yy, mm/dd/yyyy, dd/mm/yy, and dd/mm/yyyy.

1) Regex to match any of these date formats and allow leading zeros to be omitted

Regex : ^[0-3]?[0-9]/[0-3]?[0-9]/(?:[0-9]{2})?[0-9]{2}$
List dates = new ArrayList();
dates.add("1/1/11");
dates.add("01/01/11");
dates.add("01/01/2011");
dates.add("01/1/2011");
dates.add("1/11/2011");
dates.add("1/11/11");
dates.add("11/1/11");
 
String regex = "^[0-3]?[0-9]/[0-3]?[0-9]/(?:[0-9]{2})?[0-9]{2}$";
 
Pattern pattern = Pattern.compile(regex);
 
for(String date : dates)
{
    Matcher matcher = pattern.matcher(date);
    System.out.println(date +" : "+ matcher.matches());
}
 
Output:
 
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

2) Regex to match any of these date formats and require leading zeros

Regex : ^[0-3][0-9]/[0-3][0-9]/(?:[0-9][0-9])?[0-9][0-9]$
List dates = new ArrayList();
 
//With leading zeros
dates.add("01/01/11");
dates.add("01/01/2011");
 
//Missing leading zeros
dates.add("1/1/11");
dates.add("01/1/2011");
dates.add("1/11/2011");
dates.add("1/11/11");
dates.add("11/1/11");
 
String regex = "^[0-3][0-9]/[0-3][0-9]/(?:[0-9][0-9])?[0-9][0-9]$";
 
Pattern pattern = Pattern.compile(regex);
 
for(String date : dates)
{
    Matcher matcher = pattern.matcher(date);
    System.out.println(date +" : "+ matcher.matches());
}
 
Output:
 
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

3) Regex to match “mm/dd/yyyy” with required leading zeros

Regex : ^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$
List dates = new ArrayList();
//With leading zeros
dates.add("01/01/11");
dates.add("01/01/2011");
//Missing leading zeros
dates.add("1/1/11");
dates.add("01/1/2011");
 
String regex = "^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$";
 
Pattern pattern = Pattern.compile(regex);
 
for(String date : dates)
{
    Matcher matcher = pattern.matcher(date);
    System.out.println(date +" : "+ matcher.matches());
}
 
Output:
 
01/01/11 :  false
01/01/2011 :    true
1/1/11 :    false
01/1/2011 :     false

4) Regex to match “dd/mm/yyyy” with required leading zeros

Regex : ^(3[01]|[12][0-9]|0[1-9])/(1[0-2]|0[1-9])/[0-9]{4}$
List dates = new ArrayList();
//With leading zeros
dates.add("07/13/2011");
dates.add("13/07/2011");
//Missing leading zeros
dates.add("1/1/11");
dates.add("01/1/2011");
 
String regex = "^(3[01]|[12][0-9]|0[1-9])/(1[0-2]|0[1-9])/[0-9]{4}$";
 
Pattern pattern = Pattern.compile(regex);
 
for(String date : dates)
{
    Matcher matcher = pattern.matcher(date);
    System.out.println(date +" : "+ matcher.matches());
}
 
Output:
 
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 to suit your needs.

Happy Learning !!

Date Format Validation with java

Date Format Validation with java Posted on 11-08-2016  In this regex tutorial, we will learn to validate simple date formats e.g. mm/dd/yy, mm/dd/yyyy, dd/mm/yy, and dd/mm/yyyy. Here, we want to use a regex that simply checks whether the input looks like a date, without trying to eliminate things such as February 31st. 10/10 255

Comment:

To comment you must be logged in members.

Files with category

  • JUnit 5 State Of The Union using java

    View: 633    Download: 0   Comment: 0   Author: none  

    JUnit 5 State Of The Union using java

    Category: Javascript
    Fields: Other

    1.5/3 review
    JUnit 5 has been under development for about 14 months now and the prototype is almost a year old. Time to summarize what happened so far, where the project stands, and where it’s going.

  • Getting Started with Dropwizard using java

    View: 736    Download: 0   Comment: 0   Author: none  

    Getting Started with Dropwizard using java

    Category: Javascript
    Fields: Other

    1.5/3 review
    Dropwizard is a framework for building RESTful web services in Java. In this tutorial we’re going to have a look at how to get started with developing a Dropwizard application by building a new service from scratch.

  • Build Query NULL Value in MySql

    View: 340    Download: 0   Comment: 0   Author: none  

    Build Query NULL Value in MySql

    Category: Javascript
    Fields: Other

    2.5/2 review
    Misunderstanding NULL is common mistake beginners do while writing MySql query. While quering in MySql they compare column name with NULL. In MySql NULL is nothing or in simple word it isUnknown Value so if you use comparison operator for NULL values...

  • Manage Your JavaScript Application State with MobX

    View: 406    Download: 0   Comment: 0   Author: none  

    Manage Your JavaScript Application State with MobX

    Category: Javascript
    Fields: Other

    2.25/2 review
    This article was peer reviewed by Michel Weststrate and Aaron Boyer. Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be!

  • Build Bringing Pages to Life with the Web Animations API

    View: 364    Download: 0   Comment: 0   Author: none  

    Build Bringing Pages to Life with the Web Animations API

    Category: Javascript
    Fields: Other

    3/3 review
    This article is by guest author Dudley Storey. SitePoint guest posts aim to bring you engaging content from prominent writers and speakers of the JavaScript community.

  • How to Style Google Custom Search Manually

    View: 346    Download: 0   Comment: 0   Author: none  

    How to Style Google Custom Search Manually

    Category: Javascript
    Fields: Other

    0/0 review
    Website owners very often decide on using Google’s Custom Search Engine (GCSE) for searching through their content instead of using built-in and/or custom search functionality. The reason is simple – it’s much less work, and most often it does the...

  • Test React Components Using Jest

    View: 5116    Download: 0   Comment: 0   Author: none  

    Test React Components Using Jest

    Category: Javascript
    Fields: Other

    4.5/1 review
    This article is by guest author Jack Franklin. SitePoint guest posts aim to bring you engaging content from prominent writers and speakers of the JavaScript community.

  • Programming Question Reverse String without using String function

    View: 821    Download: 0   Comment: 0   Author: none  

    Programming Question Reverse String without using String function

    Category: Javascript
    Fields: Other

    4.5/1 review
    Write a program to reverse string without using string function. You don’t have to use any in-built string library function. This problem can be solved by multiple approaches. Let’s check it.

 

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd