我有一个字符串 DD/MM/YYYY
日期格式,我想检查新日期是否早于今天。我用LocalDate.now();
但是,当我运行此代码时出现异常:
LocalDate today = LocalDate.now();
DateTimeFormatter FORMATO_DIA = DateTimeFormatter.ofPattern("dd/mm/yyyy");
String otherDay = "02/12/1995";
LocalDate otherDay2 = LocalDate.parse(otherDay, FORMATO_DIA);
if (today.isBefore(otherDay2)){
System.out.println("NICE");
}
异常文本:
Exception in thread "main" java.time.format.DateTimeParseException: Text '02/12/1995' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=2, Year=1995, MinuteOfHour=12},ISO of type java.time.format.Parsed
最佳答案
这是因为您使用的是mm
,它代表分钟。您必须使用 MM
表示月份
关于java - 将 LocalDate 转换为 DD/MM/YYYY LocalDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60846635/