如何从 YearMonth 日期获取月份的最后一天?
How to get the last day of month from a YearMonth date?
我要将 YearMonth 日期转换为 LocalDate,这将是该月的最后一天。所以我需要使用 toLocalDate(int i) 方法。如何使用 yearMonth 实例获取该月的最后一天并将其传递给该方法?
除了简单地拥有 array
个数字之外,您还可以使用类似以下内容以编程方式计算出来:
if (month == 2 && year % 4 == 0)
return 29;
else if (month == 2)
return 28;
else {
if (month > 7)
month -= 1;
if (month % 2 == 0) //Month number is even
return 30;
else
return 31;
}
所以要完成这个:
- 首先,您要检查是否是二月并且是闰年。
- 接下来您只需检查是否是二月。
- 在那之后,您要查看该月份是八月还是更晚,因为那是 31 天的月份的开始时间。如果是这种情况,您基本上可以将月份数从偶数更改,反之亦然。
- 最后,你 运行 月份数(编辑与否,取决于它是八月还是更晚)看它是偶数还是奇数,最终决定最后一天。
示例:2015 年 2 月。month = 2
和 year = 2015
。 month
等于 2
但 year % 4
等于 3
。最终结果,return 28
。
示例 2:2015 年 10 月。month = 10
和 year = 2015
。 month
大于 7,所以 month = 9
。 month
是奇数,所以最终结果是 return 31
。
您可以使用 dateTime.dayOfMonth().getMaximumValue()
获取当前月份和年份的最后一天。
public class Test {
public static void main(String[] args) {
YearMonth ym = new YearMonth();
System.out.println(ym);
int dayCount = daysOfMonth(ym.getYear(), ym.getMonthOfYear());
LocalDate localDate = new LocalDate(ym.getYear(), ym.getMonthOfYear(), dayCount);
System.out.println(localDate);
}
public static int daysOfMonth(int year, int month) {
DateTime dateTime = new DateTime(year, month, 14, 12, 0, 0, 000);
return dateTime.dayOfMonth().getMaximumValue();
}
}
也可以简单地更改主要方法部分(不更改 daysOfMonth 方法)
public static void main(String[] args) {
YearMonth ym = new YearMonth();
System.out.println(ym);
LocalDate endOfMonth = ym.toLocalDate(daysOfMonth(ym.getYear(), ym.getMonthOfYear()));
System.out.println(endOfMonth);
}
最后我想到了这个解决方案:
yearMonth.toLocalDate(yearMonth.plusMonths(1).toLocalDate(1).minusDays(1).getDayOfMonth())
我需要获取下个月的第一天,然后使用 minusDays(1) 方法从中扣除 1 天。
我要将 YearMonth 日期转换为 LocalDate,这将是该月的最后一天。所以我需要使用 toLocalDate(int i) 方法。如何使用 yearMonth 实例获取该月的最后一天并将其传递给该方法?
除了简单地拥有 array
个数字之外,您还可以使用类似以下内容以编程方式计算出来:
if (month == 2 && year % 4 == 0)
return 29;
else if (month == 2)
return 28;
else {
if (month > 7)
month -= 1;
if (month % 2 == 0) //Month number is even
return 30;
else
return 31;
}
所以要完成这个:
- 首先,您要检查是否是二月并且是闰年。
- 接下来您只需检查是否是二月。
- 在那之后,您要查看该月份是八月还是更晚,因为那是 31 天的月份的开始时间。如果是这种情况,您基本上可以将月份数从偶数更改,反之亦然。
- 最后,你 运行 月份数(编辑与否,取决于它是八月还是更晚)看它是偶数还是奇数,最终决定最后一天。
示例:2015 年 2 月。month = 2
和 year = 2015
。 month
等于 2
但 year % 4
等于 3
。最终结果,return 28
。
示例 2:2015 年 10 月。month = 10
和 year = 2015
。 month
大于 7,所以 month = 9
。 month
是奇数,所以最终结果是 return 31
。
您可以使用 dateTime.dayOfMonth().getMaximumValue()
获取当前月份和年份的最后一天。
public class Test {
public static void main(String[] args) {
YearMonth ym = new YearMonth();
System.out.println(ym);
int dayCount = daysOfMonth(ym.getYear(), ym.getMonthOfYear());
LocalDate localDate = new LocalDate(ym.getYear(), ym.getMonthOfYear(), dayCount);
System.out.println(localDate);
}
public static int daysOfMonth(int year, int month) {
DateTime dateTime = new DateTime(year, month, 14, 12, 0, 0, 000);
return dateTime.dayOfMonth().getMaximumValue();
}
}
也可以简单地更改主要方法部分(不更改 daysOfMonth 方法)
public static void main(String[] args) {
YearMonth ym = new YearMonth();
System.out.println(ym);
LocalDate endOfMonth = ym.toLocalDate(daysOfMonth(ym.getYear(), ym.getMonthOfYear()));
System.out.println(endOfMonth);
}
最后我想到了这个解决方案:
yearMonth.toLocalDate(yearMonth.plusMonths(1).toLocalDate(1).minusDays(1).getDayOfMonth())
我需要获取下个月的第一天,然后使用 minusDays(1) 方法从中扣除 1 天。