提问人:James 提问时间:11/9/2023 最后编辑:Arvind Kumar AvinashJames 更新时间:11/9/2023 访问量:87
Java 周期返回 2 个日期之间的错误天数
Java Period return wrong number of days between 2 dates
问:
我有以下代码来计算 2 天之间的天数差,排除的结果是 31 天,但代码返回 1 天。01/09/2023 and 02/10/2023
LocalDate from = LocalDate.of(2023, 9, 1);
LocalDate to = LocalDate.of(2023, 10, 02);
Period period = Period.between(from, to);
System.out.print(period.getYears() + " years,");
System.out.print(period.getMonths() + " months,");
System.out.print(period.getDays() + " days");
输出:
0 years,1 months,1 days
谁能告诉我我的代码出了什么问题
答:
评论
from
to
ChronoUnit.DAYS.between(from, to);