在DD / MM / YYYY LocalDate中转换LocalDate [英] Convert LocalDate in DD/MM/YYYY LocalDate

查看:771
本文介绍了在DD / MM / YYYY LocalDate中转换LocalDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期格式为 DD / MM / YYYY 的字符串,我想检查新的日期是否早于今天。我使用 LocalDate.now(); 但是,运行此代码时出现异常:

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");
}

例外文字:

线程主中的异常java.time.format.DateTimeParseException:无法解析文本 02/12/1995:无法从TemporalAccessor获取LocalDate:{DayOfMonth = 2,Year = 1995年,MinuteOfHour = 12},ISO类型为java.time.format.Parsed

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

It's because you are using mm, which stands for minutes. You must use MM for month

这篇关于在DD / MM / YYYY LocalDate中转换LocalDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆