is Date String by Regex - Java java.util.regex

Java examples for java.util.regex:Match Date Time

Description

is Date String by Regex

Demo Code


//package com.java2s;

import java.util.regex.Pattern;

public class Main {
    public static boolean isDate(String date) {
        return isDate(date, "-");
    }//from  ww w  . jav a  2s .c om

    public static boolean isDate(String date, String partition) {
        if (date == null || partition == null || "".equals(partition))
            return false;
        String s = "";

        char split = partition.charAt(0);
        if (split != '/' && split != '/' && split != '-' && split != ' ')
            throw new IllegalArgumentException((new StringBuilder(
                    "partition can not start with '")).append(partition)
                    .append("'!").toString());
        s = (new StringBuilder(String.valueOf(s))).append(split).toString();

        StringBuilder part = new StringBuilder(
                "^((//d{2}(([02468][048])|([13579][26]))");
        part.append(s);
        part.append("((((0?[13578]");
        part.append(")|(1[02]))");
        part.append(s);
        part.append("((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[4");
        part.append("69])|(11))");
        part.append(s);
        part.append("((0?[1-9])|([1-2][0-9])|(30)))|(0?2[//-/////");
        part.append("s]?((0?[1-9])|([1-2][0-9])))))|(//d{2}(([02468][1235679])|([1");
        part.append("3579][01345789]))");
        part.append(s);
        part.append("((((0?[13578])|(1[02]))");
        part.append(s);
        part.append("((");
        part.append("0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))");
        part.append(s);
        part.append("((");
        part.append("0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))");
        part.append(s);
        part.append("((");
        part.append("0?[1-9])|([1-2][0-9])|(30)))|(0?2");
        part.append(s);
        part.append("((0?[1-9])|(1[0-9])|(2[0-8]))))))$");
        return Pattern.matches(part.toString(), date);
    }
}

Related Tutorials