gpt4 book ai didi

java - JodaTime - 2 个日期之间每月的天数

转载 作者:太空宇宙 更新时间:2023-11-04 13:48:01 27 4
gpt4 key购买 nike

我有 2 个这样的约会:

DateTime startingDate = new DateTime(STARTING_YEAR, STARTING_MONTH, STARTING_DAY, 0, 0);
DateTime endingDate = new DateTime(ENDING_YEAR, ENDING_MONTH, ENDING_DAY, 0, 0);

TOTAL_DAYS = Days.daysBetween(startingDate, endingDate).getDays();

很容易知道之间的总天数,但我对 API 一点也不熟悉,想知道是否有一种更简单的方法可以找到两个日期之间每个月的天数,而无需循环和 ifs。

示例:

DateTime startingDate = new DateTime(2000, 1, 1, 0, 0);
DateTime endingDate = new DateTime(2000, 2, 3, 0, 0);

一月为 31,二月为 2。

谢谢!

最佳答案

我终于用循环做到了。

DateTime startingDate = new DateTime(STARTING_YEAR, STARTING_MONTH, STARTING_DAY, 0, 0);
DateTime endingDate = new DateTime(ENDING_YEAR, ENDING_MONTH, ENDING_DAY, 0, 0);
TOTAL_DAYS = Days.daysBetween(startingDate, endingDate).getDays();

DateTime currentDate = startingDate;

System.out.println(currentDate.dayOfMonth().getMaximumValue() - currentDate.dayOfMonth().get() + 1);
currentDate = currentDate.plus(Period.months(1));

while (currentDate.isBefore(endingDate)) {
System.out.println(currentDate.dayOfMonth().getMaximumValue());
currentDate = currentDate.plus(Period.months(1));
}
System.out.println(endingDate.dayOfMonth().get());

关于java - JodaTime - 2 个日期之间每月的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30629715/

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