gpt4 book ai didi

java - 将 LocalDate 转换为 DD/MM/YYYY LocalDate

转载 作者:行者123 更新时间:2023-12-02 01:38:05 26 4
gpt4 key购买 nike

我有一个字符串 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/54896499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com