gpt4 book ai didi

java - Jodatime beforeOrEqual 和 afterOrEqual

转载 作者:行者123 更新时间:2023-12-01 10:13:33 27 4
gpt4 key购买 nike

我正在使用 jodatime (java 6) 来检查某个时间间隔内的日期,我确实需要更有效地完成它。有没有办法避免这种困惑:(所有变量的类型都是org.joda.time.LocalDate)

    if ((startReservation.isBefore(beginInterval) || startReservation.equals(beginInterval))
&& ((endReservation.isAfter(beginInterval) && (endReservation.isBefore(finishInterval) || endReservation.equals(finishInterval))))) {

startReservation= beginInterval;
//do stuff
}

还有一堆其他else if

jodatime 是否实现了 beforeOrEqual()/afterOrEqual() 方法或类似方法?

最佳答案

假设您的变量是ReadableInstant类型的对象,则表达式

startReservation.isBefore(beginInterval) || startReservation.equals(beginInterval)

相当于更简单、更短的表达式:

!startReservation.isAfter(beginInterval)

重要提示:如果您的时刻(尤其是 DateTime)使用不同的时区,则使用 equals() 方法可能会有害。建议的解决方法更安全。

Joda-Time 没有实现诸如 beforeOrEqual() 等方法,但解决方法非常简单,以至于 Joda-Time-Team 尚未考虑此增强功能。 所以你终于可以做到:

if (
!startReservation.isAfter(beginInterval)
&& ((endReservation.isAfter(beginInterval)
&& (!endReservation.isAfter(finishInterval)
) {
startReservation= beginInterval;
//do stuff
}

旁注:从数学上讲,这就像艾伦区间关系“starts”、“finishedBy”、“overlaps”和“equals”(等价)的组合。

关于java - Jodatime beforeOrEqual 和 afterOrEqual,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36033833/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com