【发布时间】:2022-02-15 00:42:25
【问题描述】:
我尝试使用dd/MM/yyyy 格式检查String 输入是否是有效日期,如下所示:
String input = Scanner.nextLine();
DateTimeFormatter formater = DateTimeFormatter.ofPattern("dd/MM/yyyy");
try{
LocaleDate.parse(input, formater);
}
catch(Exception e)
但它无法检查以下一些规则:
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.
当我使用input = "30/02/2022" 时,它是合法的。
我使用netbeans 8.2 和jdk 1.8。他们是否支持一些检查这些规则的方法?
【问题讨论】: