小编hoà*_*yễn的帖子

检查java中的有效日期

我尝试使用如下String格式检查输入是否为有效日期:dd/MM/yyyy

String input = Scanner.nextLine();
DateTimeFormatter formater = DateTimeFormatter.ofPattern("dd/MM/yyyy");
try{
    LocaleDate.parse(input, formater);
}
catch(Exception e)
Run Code Online (Sandbox Code Playgroud)

但它无法检查以下一些规则:

Leap year, February 29 days.

Common year, February 28 days.

Month 1, 3, 5, 7, 8, 10, 12, max 31 days.

Month 4, 6, 9, 11, max 30 days.
Run Code Online (Sandbox Code Playgroud)

当我使用时input = "30/02/2022",它是合法的。我用netbeans 8.2jdk 1.8。他们支持一些检查这些规则的方法吗?

java date

6
推荐指数
1
解决办法
686
查看次数

标签 统计

date ×1

java ×1