英文:
How to find YearMonth is equal or after current month?
问题
I want to find the month is equal or after the current YearMonth
final YearMonth yearMonth = YearMonth.of(2020, 8);
final boolean after = yearMonth.isAfter(YearMonth.now());
Here current month is August(8) and I tried isAfter which returns false.
I want to return true for current month also.
Is any method is there in YearMonth itself?
我想找到月份是否等于或晚于当前的YearMonth
final YearMonth yearMonth = YearMonth.of(2020, 8);
final boolean after = yearMonth.isAfter(YearMonth.now());
这里当前月份是八月(8),我尝试使用isAfter返回false。
我想对当前月份也返回true。
YearMonth本身是否有任何方法可以做到这一点?
英文:
I want to find the month is equal or after the current YearMonth
final YearMonth yearMonth = YearMonth.of(2020, 8);
final boolean after = yearMonth.isAfter(YearMonth.now());
Here current month is August(8) and I tried isAfter which returns false.
I want to return true for current month also.
Is any method is there in YearMonth itself?
答案1
得分: 8
你可以只使用isBefore
方法的否定形式:
final boolean equalOrAfter = !yearMonth.isBefore(YearMonth.now());
毕竟,YearMonth可以在另一个YearMonth之前、之后或等于另一个YearMonth,所以如果一个YearMonth不在另一个YearMonth之前,它必须要么等于它,要么在它之后。
英文:
You can just use the negation of the isBefore
method:
final boolean equalOrAfter = !yearMonth.isBefore(YearMonth.now());
After all a YearMonth can either be before, after or equal to another YearMonth, so if a YearMonth is not before another YearMonth it has to be either equal to or after it.
答案2
得分: -1
Sure, here's the translated code:
return yearMonth.getMonth().getValue() >= YearMonth.now().getMonth().getValue();
英文:
return yearMonth.getMonth().getValue()>= YearMonth.now().getMonth().getValue();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论