正如我在评论中已经提到的那样,=>
对于操作员来说,问题是因为符号不好。应该是>=
。检查此以了解有关运算符的更多信息。
除此之外,我可以看到您的逻辑存在严重问题。您验证日期值的方式是一种天真的方式。我建议您使用 OOTB API 来执行此操作,如下所示:
import java.time.DateTimeException;
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
// Tests
checkAgeFormat(45, 10, 2017, 10, 10, 2010);
checkAgeFormat(30, 2, 2017, 10, 10, 2010);
checkAgeFormat(31, 4, 2017, 10, 10, 2010);
checkAgeFormat(30, 15, 2017, 10, 10, 2010);
checkAgeFormat(30, 4, 2020, 10, 10, 2010);
}
static void checkAgeFormat(int current_day, int current_month, int current_year, int birth_day, int birth_month,
int birth_year) {
int f = 0;
LocalDate currentDate, birthDate;
try {
currentDate = LocalDate.of(current_year, current_month, current_day);
birthDate = LocalDate.of(birth_year, birth_month, birth_day);
System.out.println("If you see this line printed, the date values are correct.");
// ...Rest of the code
} catch (DateTimeException e) {
System.out.println(e.getMessage());
f = 1;
}
// ...Rest of code
}
}
输出:
Invalid value for DayOfMonth (valid values 1 - 28/31): 45
Invalid date 'FEBRUARY 30'
Invalid date 'APRIL 31'
Invalid value for MonthOfYear (valid values 1 - 12): 15
If you see this line printed, the date values are correct.