Friday 22 December 2017

How to write a regex to validate the date format | Regex in java


Click here to watch on Youtube :
https://www.youtube.com/watch?v=BrHA4YmEgc0&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
How to write a regex to validate the date format | Regex in java
DateValidator.java

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * How to validate date with regular expression
 */

public class DateValidator
{

    private Pattern pattern;
    private Matcher matcher;

    private static final String DATE_REGEX = "(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)";

    public DateValidator()
    {
        pattern = Pattern.compile(DATE_REGEX);
    }

    /*
     * 1.Date is valid against Regex.
     * 2.Days of the month is valid.
     */

    public boolean validate(final String date)
    {

        matcher = pattern.matcher(date);

        if (matcher.matches())
        {

            matcher.reset();

            if (matcher.find())
            {

                String day = matcher.group(1);
                String month = matcher.group(2);
                int year = Integer.parseInt(matcher.group(3));

                if (day.equals("31") && (month.equals("4") || month.equals("6")
                        || month.equals("9") || month.equals("11")
                        || month.equals("04") || month.equals("06")
                        || month.equals("09")))
                {
                    return false; // only 1,3,5,7,8,10,12 has 31 days
                }
                else if (month.equals("2") || month.equals("02"))
                {
                    // leap year
                    if (year % 4 == 0)
                    {
                        if (day.equals("30") || day.equals("31"))
                        {
                            return false;
                        }
                        else
                        {
                            return true;
                        }
                    }
                    else
                    {
                        if (day.equals("29") || day.equals("30")
                                || day.equals("31"))
                        {
                            return false;
                        }
                        else
                        {
                            return true;
                        }
                    }
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
}

RegexDemo.java

import java.util.Arrays;
import java.util.List;

/**
 *
 * How to validate date with regular expression
 *
 */

public class RegexDemo
{

    public static void main(String[] args)
    {
        List<String> validDateList = Arrays.asList("01/01/2020", "31/01/2020",
                "31/8/2010", "1/1/2010");

        DateValidator dateValidator = new DateValidator();

        for (String date : validDateList)
        {
            System.out.println(
                    date + " is Valid? = " + dateValidator.validate(date));
        }

        System.out.println("--------------------------------------");

        List<String> inValidDateList = Arrays.asList("32/1/2010", "31/9/2010",
                "333/2/200f", "110:20");

        for (String date : inValidDateList)
        {
            System.out.println(
                    date + " is Valid? = " + dateValidator.validate(date));
        }


    }

}

Output

01/01/2020 is Valid? = true
31/01/2020 is Valid? = true
31/8/2010 is Valid? = true
1/1/2010 is Valid? = true
--------------------------------------
32/1/2010 is Valid? = false
31/9/2010 is Valid? = false
333/2/200f is Valid? = false
110:20 is Valid? = false

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_date_format.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_date_format

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/576579fab6c376d5ecd7cb5b7402b7fd1a7c4ae3/BasicJava/RegexDemo_date_format/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment