- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
我正在使用 jodatime (java 6) 来检查某个时间间隔内的日期,我确实需要更有效地完成它。有没有办法避免这种困惑:(所有变量的类型都是org.joda.time.LocalDate)
我是一名优秀的程序员,十分优秀!