简体   繁体   English

在Java中的String.matches中使用正则表达式以匹配日期格式

[英]Using Regex in String.matches in java to match a date format

Validating the given date against the format dd/mm/yyyy. 根据格式dd / mm / yyyy验证给定日期。
Valid = 1 有效= 1
Not Valid = -1 无效= -1
Example1: 范例1:
Input= 12/06/1987 输入= 12/06/1987
output=1 输出= 1
Example2: 范例2:
Input= 03/1/1987 输入= 03/1/1987
output=-1 输出= -1

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CheckDateFormat {
    public static void main(String[] args) {
        String s1="29/02/2006";
        getvalues(s1);
    }
    public static void getvalues(String s1) {
        if(s1.matches("[0-9]{2}[/][0-9]{2}[/][0-9]{4}"))
        {
            SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
            sdf.setLenient(false);
            try {
                Date d1=sdf.parse(s1);
                System.out.println(1);
            } catch (ParseException e) {
                //e.printStackTrace();//Always going to catch block
                System.out.println(-1);
            }
        }
        else
            System.out.println(-1);
    }
}


The problem with the code is that it it always returns -1. 该代码的问题在于它总是返回-1。
It always enters the catch block and gives a ParseException. 它总是进入catch块并给出ParseException。
Is there any problem with the Regex? 正则表达式有问题吗?

Problem is this line: 问题是这一行:

sdf.setLenient(false);

and this date: 这个日期:

String s1="29/02/2006";

As 2006 wasn't a leap year making your date invalid and with lenient set to false date parse call is failing and throwing ParseException . 由于2006 年不是a年,因此您的日期无效, 宽容设置为假日期,解析调用失败并抛出ParseException

Problem will be fixed if you comment out sdf.setLenient(false); 如果您将sdf.setLenient(false);注释掉,问题将得到解决sdf.setLenient(false); line: 线:

or use a valid date: 或使用有效日期:

String s1="29/02/2008";

Since 2008 was a leap year making 29th Feb a valid date. 由于2008是a年,因此2月29日为有效日期。

You do not need a regex. 您不需要正则表达式。 If you get a ParseException you can return -1 else you return 1 : 如果获得ParseException ,则可以return -1否则return 1

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CheckDateFormat {
    public static void main(String[] args) {
        String s1="29/02/2006";
        System.out.println(getvalues(s1));
    }
    public static int getvalues(String s1) {
        SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
        sdf.setLenient(false);
        try {
            Date d1=sdf.parse(s1);
            System.out.println(1);
        } catch (ParseException e) {
            return -1;
        }
        return 1;
    }
}

Exception is thrown due to following line. 由于下一行而引发异常。 Comment it off. 评论它。

sdf.setLenient(false); sdf.setLenient(false);

As the year 2006 was not leap year, using String s1="29/02/2006"; 因为2006年不是leap年,所以使用String s1="29/02/2006"; will throw a parse exception. 将引发解析异常。

Try with other dates it will work fine. 尝试其他日期,它将正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM