如何找到YearMonth等于或在当前月份之后?

huangapple go评论76阅读模式
英文:

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();

huangapple
  • 本文由 发表于 2020年8月5日 22:38:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63267547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定