documentation for YearMonth
对于如何处理这个问题没有任何建议。
目前,我正在使用以下方法来检查 LocalDate
是否属于 YearMonth
,其中 date
和 yearMonth
分别是这些值:
YearMonth lastDayOfPreviousMonth = yearMonth.atDay(1).minusDay(1);
YearMonth firstDayOfNextMonth = yearMonth.atEndOfMonth().plusDays(1);
return date.isAfter(lastDayOfPreviousMonth)
&& date.isBefore(firstDayOfNextMonth);
有没有更干净或内置的方法可以做到这一点?
最佳答案
正如 @cricket_007 在问题评论中所指出的,检查 LocalDate
是否属于 YearMonth
的更简洁方法是获取 YearMonth
date
的 code>,然后将其与原始的 yearMonth
进行比较:
return YearMonth.from(date).equals(yearMonth);
关于java - 查找特定 LocalDate 是否属于 YearMonth 的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44616603/