问题
I have a String with DD/MM/YYYY
date format, and I want to check if the new date is older than today. I use LocalDate.now();
However, i have an exception when I run this code:
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 text:
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
回答1:
It's because you are using mm
, which stands for minutes. You must use MM
for month
来源:https://stackoverflow.com/questions/54896499/convert-localdate-in-dd-mm-yyyy-localdate