Java Regex Date Vaidate dateOfAny(final String value)

Here you can find the source of dateOfAny(final String value)

Description

date Of Any

License

Apache License

Parameter

Parameter Description
value yyyy-MM-dd [hh:mm:ss.SSS][AM|PM]

Declaration

public static Date dateOfAny(final String value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;
import java.util.Date;

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

public class Main {
    /**// w ww .j av  a  2  s . c om
     * 
     * @param value yyyy-MM-dd [hh:mm:ss.SSS][AM|PM]
     * @return
     */
    public static Date dateOfAny(final String value) {
        if (value == null || value.trim().length() == 0)
            return null;
        Calendar calendat = Calendar.getInstance();
        final String datePattern = "[0-9]{4}-([0-1]?[0-9])-(([0-3]?[0-9])|([3][0-1]))";
        final String timePattern = "(([0-1]?[0-9])|([2][0-3]))((:([0-5]){0,1}([0-9]){1}){1,2})";
        final String timesPattern = " ([0-1]?[0-9]){1}(((:([0-5]){0,1}([0-9]){1}){1,2}) (([AP]M)|([ap]m)))?";
        Pattern p = Pattern.compile(datePattern);
        Matcher m = p.matcher(value);
        m.find();
        final String date_String = m.group();
        if (date_String != null) {
            final String[] test = date_String.split("-");
            if (test.length == 3) {
                calendat.set(Calendar.YEAR, Integer.parseInt(test[0].trim()));
                calendat.set(Calendar.MONTH, Integer.parseInt(test[1].trim()) - 1);
                calendat.set(Calendar.DAY_OF_MONTH, Integer.parseInt(test[2].trim()));
            }
        }

        if ((value.indexOf("pm") != -1) || (value.indexOf("PM") != -1) || (value.indexOf("am") != -1)
                || (value.indexOf("AM") != -1)) {
            p = Pattern.compile(timesPattern);
            m = p.matcher(value);
            m.find();
            String time_String = m.group();
            if (time_String != null) {
                final String apm = time_String.substring(time_String.trim().indexOf(" ") + 1);
                if (apm.trim().equalsIgnoreCase("pm")) {
                    time_String = time_String.trim().substring(0, time_String.trim().indexOf(" "));
                    final String[] test = time_String.split(":");
                    if (test.length == 3) {
                        calendat.set(Calendar.HOUR_OF_DAY, Integer.parseInt(test[0].trim()) + 12);
                        calendat.set(Calendar.MINUTE, Integer.parseInt(test[1].trim()));
                        calendat.set(Calendar.SECOND, Integer.parseInt(test[2].trim()));
                    }
                    if (test.length == 2) {
                        calendat.set(Calendar.HOUR_OF_DAY, Integer.parseInt(test[0].trim()) + 12);
                        calendat.set(Calendar.MINUTE, Integer.parseInt(test[1].trim()));
                        calendat.set(Calendar.SECOND, 0);
                    }
                } else {
                    final String[] test = time_String.split(":");
                    calendat = setTime(calendat, test);
                }
            }
        } else {
            p = Pattern.compile(timePattern);
            m = p.matcher(value);
            final Boolean isFind = m.find();
            if (isFind == true) {
                final String time_String = m.group();
                if (time_String != null) {
                    final String[] test = time_String.split(":");
                    calendat = setTime(calendat, test);
                }
            } else {
                calendat.set(Calendar.HOUR_OF_DAY, 0);
                calendat.set(Calendar.MINUTE, 0);
                calendat.set(Calendar.SECOND, 0);
            }

        }
        return calendat.getTime();
    }

    private static Calendar setTime(final Calendar calendat, final String[] test) {
        if (test.length == 3) {
            calendat.set(Calendar.HOUR_OF_DAY, Integer.parseInt(test[0].trim()));
            calendat.set(Calendar.MINUTE, Integer.parseInt(test[1].trim()));
            calendat.set(Calendar.SECOND, Integer.parseInt(test[2].trim()));
        }
        if (test.length == 2) {
            calendat.set(Calendar.HOUR_OF_DAY, Integer.parseInt(test[0].trim()));
            calendat.set(Calendar.MINUTE, Integer.parseInt(test[1].trim()));
            calendat.set(Calendar.SECOND, 0);
        }
        return calendat;
    }
}

Related

  1. checkDate(String date)
  2. checkDate(String strDate, String dateFmt)
  3. checkDateYYYYMMMDD(String date)
  4. dateMatcher(String dt)
  5. datePatternPattern(String appenderPrefix)
  6. dateTimeMatcher(String dt)
  7. dateTimePattern()
  8. dateToString(Date date)