日期格式yyyy / MM / dd的正则表达式模式

时间:2014-06-10 10:45:36

标签: java regex

我需要使用正则表达式模式验证格式为yyyy/MM/dd的日期。我已经有格式dd/MM/yyyy的正则表达式。

(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)

我正在使用此链接http://www.mkyong.com/regular-expressions/how-to-validate-date-with-regular-expression中的验证。我需要yyyy / MM / dd的验证。任何人都可以帮助我吗?

8 个答案:

答案 0 :(得分:2)

我找到了答案:

((?:19|20)\\d\\d)/(0?[1-9]|1[012])/([12][0-9]|3[01]|0?[1-9])

答案 1 :(得分:1)

老问题,我知道,但是因为我有理由这样做:

\d{4}\\\d{2}\\\d{2}

看起来比其他建议更贴近我的眼睛,并且在20世纪/ 21世纪之外工作(不清楚OP是否要求这样做)。

但是,我的实现将匹配日期,例如9999/99/99(显然无效) - 但是作为评论者建议,SimpleDateFormat是我认为的那种验证的工具。

如果OP能够使用SimpleDateFormat以及正则表达式,那么这就足够了:

public static final boolean isDateValid(String date) {
    if (!date.matches("\d{4}\\\d{2}\\\d{2}"))
        return false;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    try {
        sdf.parse(date);
        return true;
    } catch (ParseExceptione) {
        return false;
    }
}

NB。关于SimpleDateFormat的常见警告不是线程安全等。

答案 2 :(得分:0)

以下是我的看法:

((19|20)[0-9]{2})/((0?[1-9])|1[012])/((0?[1-9])|(1[0-9])|(3[01]))

答案 3 :(得分:0)

我认为这对你有帮助...

var regdate = /^(19[0-9][0-9]|20[0-9][0-9])\/(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])$/;

答案 4 :(得分:0)

简单的正则表达式加上SimpleDateFormat不会过滤像“2014 \ 26 \ 26”这样的字符串日期,那么这是不够的。

所以也许最好的方法是完全严格的正则表达式模式。

如果您从mkyong网站上翻转你的正则表达式(Java)表达式,你应该完成。正如你已经建议的那样:

String pattern = "((?:19|20)\\d\\d)/(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])";

为了额外保证,您可以添加SimpleDateFormat。这是我额外的冗长方法:

if(stringDate.length()==10 && stringDate.matches("((?:19|20)\\d\\d)/(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])")){      
        Date d=null;
        String checkDate=null;
        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");

        try {
            //Also parses the String as a Date with the format "yyyy/MM/dd"
            //to be sure that the passed string is correct
            d=df.parse(stringDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (d != null) {
            //Transforms the Date into a String with the format "yyyy/MM/dd"
            checkDate=df.format(d);
            System.out.println(checkDate);
        }
    }

答案 5 :(得分:0)

这是另一种简单的方法,编写一个将字符串输入转换为日期并将日期转换回字符串的小方法。然后比较两个字符串是否相等。如果相等则日期是正确的,否则其不正确。所有验证都由这种方法处理。

import java.text.SimpleDateFormat;
import java.util.Date;

public class TestDate {

    public static boolean isValidDateFormat(String value, String format) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            date = sdf.parse(value);
            if (!value.equals(sdf.format(date))) {
                date = null;
            }
        } catch (Exception ex) {
            return false;
        }
        return date != null;
    }

    public static void main(String[] args) {
        String format = "yyyy/MM/dd";
        System.out.println("2017/02/31 isValidDateFormat: " + isValidDateFormat("2017/02/31", format));
        System.out.println("2017/03/31 isValidDateFormat: " + isValidDateFormat("2017/03/31", format));
        System.out.println("2017/04/31 isValidDateFormat: " + isValidDateFormat("2017/04/31", format));
        System.out.println("2017/13/31 isValidDateFormat: " + isValidDateFormat("2017/04/31", format));
        System.out.println("2017/01/35 isValidDateFormat: " + isValidDateFormat("2017/01/35", format));
        System.out.println("017/01/30 isValidDateFormat: " + isValidDateFormat("017/01/30", format));
        // output : 
        //2017/02/31 isValidDateFormat: false
        // 2017/03/31 isValidDateFormat: true
        // 2017/04/31 isValidDateFormat: false
        // 2017/13/31 isValidDateFormat: false
        // 2017/01/35 isValidDateFormat: false
        // 017/01/30 isValidDateFormat: false
    }
}

答案 6 :(得分:0)

为了好玩,我想看看正则表达式Jon Skeet的非常有效评论(正确处理闰年正确的正则表达式)会是什么样子。看 - 他是对的;)

^
(?:(?:19|2[01])\d\d\/(?:1[02]|0[13578])\/(?:[0-2]\d|3[01]))   # 31 day months
|
(?:(?:19|2[01])\d\d\/(?:(?:11|0[469])\/(?:[0-2]\d|30)))       # 30 day months
|
(?:(?:19|2[01])(?:[02468][1235679]|[13579][01345789])|1900|2100)\/02\/(?:[01]\d|2[0-8]) # Non leap year
|
(?:(?:(?:19|21)(?!00)|20)(?:[02468][048]|[13579][26]))\/02\/(?:[01]\d|2[0-9]) # Leap year
$

See it here at regex101

它使用四种替代方法进行测试:

  • 31个月
  • 30个月
  • 二月非闰年
  • 二月闰年

请注意,闰年是四年的倍数。该规则的一个例外是100 的倍数,除非,如果它是400的倍数。(P ......这就是它的可怕之处。)

答案 7 :(得分:-1)

它的正则表达式日期格式(dd.MM.yyyy)。你可以为你调整这个。 (((0[1-9]{1}|1[0-9]|2[0-9]).(0[1-9]|1[0-2]))|(30.(04|06|09|11))|((30|31).(01|03|05|07|08|10|12))).[0-9]{4}

相关问题