2017-06-18 100 views
0

對於如何處理這個,documentation for YearMonth沒有任何建議。查找特定LocalDate是否屬於YearMonth的最簡單方法是什麼?

目前我使用下面的方法來檢查LocalDate是否在YearMonth,其中dateyearMonth分別對這些值內:

YearMonth lastDayOfPreviousMonth = yearMonth.atDay(1).minusDay(1); 
YearMonth firstDayOfNextMonth = yearMonth.atEndOfMonth().plusDays(1); 
return date.isAfter(lastDayOfPreviousMonth) 
       && date.isBefore(firstDayOfNextMonth); 

是否有任何清潔劑或在建的方式去做這個?

+2

爲什麼你不能簡單地比較月份值? –

+2

因爲我沒有在箱子外面思考?這可能是一個更清晰的方法:雖然,我不得不比較月份和年份值! –

+2

這導致我認爲'YearMonth.from(date).equals(yearMonth)'可能是最簡單的方法! –

回答

5

更清潔的方式來檢查YearMonthLocalDate跌倒是否得到dateYearMonth,然後比較,爲原來的yearMonth

return YearMonth.from(date).equals(yearMonth); 
相關問題