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

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

Description

is Date Time by Regex

Demo Code


//package com.java2s;

import java.util.regex.Pattern;

public class Main {
    public static boolean isDateTime(String dateTime) {
        return isDateTime(dateTime, "-");
    }/*from w  w w .  ja  va2 s.  c om*/

    public static boolean isDateTime(String dateTime, String partition) {
        if (dateTime == 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]))))))");
        part.append("(//s(((0?[0-9])|([1-2][0-3]))//:([0-5]?[0-9])//:([0-5]?[0-9])))?");
        return Pattern.matches(part.toString(), dateTime);
    }
}

Related Tutorials