Java Regex Date Vaidate parseDate(String entry)

Here you can find the source of parseDate(String entry)

Description

parse Date

License

Apache License

Declaration

public static String parseDate(String entry) 

Method Source Code

//package com.java2s;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * Copyright 2013 Joseph Yuan//w  w  w  .jav  a2 s  . c o m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

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

public class Main {
    public final static String DATE_REGEX_4_DIGIT_YEAR = "("
            + "(19|20)[0-9]{2}" + "([-/.\\\\]{1})"
            + "[0?[1-9]|[1-9]|1[012]]{1,2}" + "\\3"
            + "([0?[1-9]|[1-9]|1[0-9]|2[0-9]|3[01]]{1,2})" + ")" + "|"
            + "(" + "[0?[1-9]|[1-9]|1[012]]{1,2}" + "([-/.\\\\]{1})"
            + "([0?[1-9]|[1-9]|1[0-9]|2[0-9]|3[01]]{1,2})" + "\\6"
            + "(19|20)[0-9]{2}" + ")";
    public final static String DATE_REGEX_2_DIGIT_YEAR = "(" + "[0-9]{2}"
            + "([-/.\\\\]{1})" + "[0?[1-9]|[1-9]|1[012]]{1,2}" + "\\3"
            + "([0?[1-9]|[1-9]|1[0-9]|2[0-9]|3[01]]{1,2})" + ")" + "|"
            + "(" + "[0?[1-9]|[1-9]|1[012]]{1,2}" + "([-/.\\\\]{1})"
            + "([0?[1-9]|[1-9]|1[0-9]|2[0-9]|3[01]]{1,2})" + "\\6"
            + "[0-9]{2}" + ")";

    public static String parseDate(String entry) {
        Pattern pattern = Pattern.compile(DATE_REGEX_4_DIGIT_YEAR);
        String[] terms = entry.split(" ");
        Matcher matcher;
        for (String term : terms) {
            matcher = pattern.matcher(term);
            while (matcher.matches()) {
                return matcher.group();
            }
        }
        pattern = Pattern.compile(DATE_REGEX_2_DIGIT_YEAR);
        terms = entry.split(" ");
        for (String term : terms) {
            matcher = pattern.matcher(term);
            while (matcher.matches()) {
                return matcher.group();
            }
        }
        return "";
    }
}

Related

  1. dateTimePattern()
  2. dateToString(Date date)
  3. parseDate(final String date, final DateTimeFormatter parser)
  4. parseDate(String date)
  5. parseDate(String dateStr)
  6. parseDate(String text)
  7. parseDateDiff(final String time)
  8. parseDateDiff(String time)
  9. parseDateDiff(String time, boolean future)