我将把YearMonth日期转换为LocalDate,这将是该月的最后一天。 所以我需要使用toLocalDate(int i)方法。 如何使用yearMonth实例将该月的最后一天传递给该方法?
I am going to convert a YearMonth date to a LocalDate which is going to be the last day for that month. So I need to use the toLocalDate(int i) method. How can I get the last day of that month using the yearMonth instance to pass it to that method?
最满意答案
如果只是拥有一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; }所以要走过这个:
首先,你要检查是否是2月,这是闰年。 接下来你只是检查它是否是2月份。 之后,您正在查看月份是八月还是更晚,因为那是31天的月份开始。 如果是这种情况,您基本上可以从偶数更改月份数,反之亦然。 最后,您运行月份编号(编辑与否,取决于是否为8月或更晚),以查看它是偶数还是奇数,最终决定最后一天。示例: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 。
Finally I came up with this solution:
yearMonth.toLocalDate(yearMonth.plusMonths(1).toLocalDate(1).minusDays(1).getDayOfMonth())
I need to get the first day of the next month and then deducting 1 day from it using minusDays(1) method.
更多推荐
发布评论