java 使用java验证信用卡到期日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11528949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:27:24  来源:igfitidea点击:

validate the credit card expiry date using java?

javaregexvalidationdate

提问by Preethi

I am looking to validate the credit card expiry date in MM/YY format . I have no clue on how to validate , whether to go for Simple date format / Regex .

我希望以 MM/YY 格式验证信用卡到期日期。我不知道如何验证,是否使用简单日期格式/正则表达式。

Appreciate your help.

感谢你的帮助。

回答by Bohemian

Use SimpleDateFormatto parse a Date, then compare it with a new Date, which is "now":

使用SimpleDateFormat解析一个Date,然后将它与一个新的比较Date,这是“现在”:

String input = "11/12"; // for example
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/yy");
simpleDateFormat.setLenient(false);
Date expiry = simpleDateFormat.parse(input);
boolean expired = expiry.before(new Date());

Edited:

编辑:

Thanks to @ryanpfor the leniency aspect. The above code will now throw a ParseExceptionif the input is not proper.

感谢您@ryanp的宽大处理。ParseException如果输入不正确,上面的代码现在将抛出一个。

回答by ryanp

Playing devil's advocate...

扮演魔鬼代言人...

boolean validateCardExpiryDate(String expiryDate) {
    return expiryDate.matches("(?:0[1-9]|1[0-2])/[0-9]{2}");
}

which translates as:

翻译成:

...so this version requires zero-padded months (01 - 12). Add a ?after the first 0to prevent this.

...所以这个版本需要零填充月份(01 - 12)。?在第一个之后添加一个0以防止这种情况。

回答by Ole V.V.

java.time

时间

To validate whether you've got a valid expiration date string:

要验证您是否有有效的到期日期字符串:

    DateTimeFormatter ccMonthFormatter = DateTimeFormatter.ofPattern("MM/uu");
    String creditCardExpiryDateString = "11/21";
    try {
        YearMonth lastValidMonth = YearMonth.parse(creditCardExpiryDateString, ccMonthFormatter);
    } catch (DateTimeParseException dtpe) {
        System.out.println("Not a valid expiry date: " + creditCardExpiryDateString);
    }

To validate whether it denotes an expired credit card:

要验证它是否表示过期的信用卡:

        if (YearMonth.now(ZoneId.systemDefault()).isAfter(lastValidMonth)) {
            System.out.println("Credit card has expired");
        }

Think about which time zone you want to use since the new month doesn't begin at the same moment in all time zones. If you wanted UTC:

考虑您要使用哪个时区,因为新月份并非在所有时区的同一时刻开始。如果你想要UTC:

        if (YearMonth.now(ZoneOffset.UTC).isAfter(lastValidMonth)) {

If you wanted, for the sake of the example, Europe/Kiev time zone:

如果你想,举个例子,欧洲/基辅时区:

        if (YearMonth.now(ZoneId.of("Europe/Kiev")).isAfter(lastValidMonth)) {

Link:Oracle tutorial: Date Timeexplaining how to use java.time.

链接:Oracle 教程:解释如何使用 java.time 的日期时间

回答by Oleg Koltunov

I think that code will be better:

我认为代码会更好:

int month = 11;
int year = 2012;

int totalMonth = (year * 12) + month;
totalMonth++; // next month needed
int nextMonth = totalMonth % 12;
int yearOfNextMonth = totalMonth / 12;

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/yyyy");
simpleDateFormat.setLenient(false);
Date expiry = simpleDateFormat.parse(nextMonth + "/" + yearOfNextMonth);
boolean expired = expiry.before(new Date());

You need calculate next month because month displayed on credit card is the last month when card is valid.

您需要计算下个月,因为信用卡上显示的月份是卡有效的最后一个月。

回答by MRAB

Do you really need to use regex? Regex is really only suitable for matching characters, not dates. I think it would be far easier to just use simple date functions.

你真的需要使用正则表达式吗?正则表达式真的只适用于匹配字符,而不适用于日期。我认为只使用简单的日期函数会容易得多。