Convert LocalDate in DD/MM/YYYY LocalDate [duplicate]

烈酒焚心 提交于 2019-12-02 19:22:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!