时间比较
Comparison of time
我无法理解,或者说无法证明:
(isAfter(x) or isEquals(x)) == !isBefore(x)
和
(isBefore(x) or isEquals(x)) == !isAfter(x)
陈述总是正确的
与
default boolean isAfter(ChronoZonedDateTime<?> other) {
long thisEpochSec = toEpochSecond();
long otherEpochSec = other.toEpochSecond();
return thisEpochSec > otherEpochSec ||
(thisEpochSec == otherEpochSec && toLocalTime().getNano() > other.toLocalTime().getNano());
}
default boolean isBefore(ChronoZonedDateTime<?> other) {
long thisEpochSec = toEpochSecond();
long otherEpochSec = other.toEpochSecond();
return thisEpochSec < otherEpochSec ||
(thisEpochSec == otherEpochSec && toLocalTime().getNano() < other.toLocalTime().getNano());
}
default boolean isEqual(ChronoZonedDateTime<?> other) {
return toEpochSecond() == other.toEpochSecond() &&
toLocalTime().getNano() == other.toLocalTime().getNano();
}
来自 java.time.chrono.ChronoZonedDateTime;
阅读条件,
(isBefore(x) or isEquals(x)) == !isAfter(x)
如果你是 before
或 equal
你就不是 after
。
示例:
如果你在第二名或之后,你就不能成为第一名。
如果两个条件都为真,则意味着您是 before
、equal
或 after
。因为你不可能是 after
和 before
(除非你是盒子里的猫),所以你是 equal
.
代码解释
这些条件很简单,首先它会尝试将以秒为单位的时间与toEpochSecond()
进行比较。
然后,根据方法的不同,它会检查纳秒值以更精确。这部分将取决于方法。让我们来一个:
default boolean isAfter(ChronoZonedDateTime<?> other) {
long thisEpochSec = toEpochSecond();
long otherEpochSec = other.toEpochSecond();
return thisEpochSec > otherEpochSec ||
(thisEpochSec == otherEpochSec && toLocalTime().getNano() > other.toLocalTime().getNano());
}
要检查 this
是否在 other
之后,我们需要检查 this.seconds
是否大于 other.seconds
。
thisEpochSec > otherEpochSec
如果不是,它会更深入,只有在秒数相等时才会检查到纳秒
thisEpochSec == otherEpochSec // seconds equals
&& toLocalTime().getNano() > other.toLocalTime().getNano());
如果您的问题是您是否可以证明 isBefore
、isEqual
和 isAfter
中的一个是正确的——是的,您可以。
this.toEpochSecond()
和 other.toEpochSecond()
之间存在三种可能的比较:<
、==
或 >
。看逻辑,很明显,如果比较的是<
,那么isBefore()
为真,isAfter()
和isEqual()
为假,因为>
和==
对 toEpochSecond
的比较将是错误的。同样,如果比较是 >
,则 isAfter()
为真,而 isBefore()
和 isEqual()
为假。所以在这两种情况下,这个命题成立。
第三种情况呢,toEpochSecond
结果相等?然后我们比较 toLocalTime().getNano()
,它又可以是 <
、==
或 >
。如果我们查看代码在 toEpochSecond
结果相等和比较 getNano
结果时的行为方式,我们发现在所有三种情况下,只有一种方法 return 为真,而其他方法return 错误。
所以从本质上讲,我们已经研究了五种可能的情况,在所有情况下,这三种方法中只有一种是正确的。由于我们已经证明了这一点,因此您原来问题中的陈述是:
(isAfter(x) or isEquals(x)) == !isBefore(x)
(isBefore(x) or isEquals(x)) == !isAfter(x)
也是如此。 QED
我无法理解,或者说无法证明:
(isAfter(x) or isEquals(x)) == !isBefore(x)
和
(isBefore(x) or isEquals(x)) == !isAfter(x)
陈述总是正确的
与
default boolean isAfter(ChronoZonedDateTime<?> other) {
long thisEpochSec = toEpochSecond();
long otherEpochSec = other.toEpochSecond();
return thisEpochSec > otherEpochSec ||
(thisEpochSec == otherEpochSec && toLocalTime().getNano() > other.toLocalTime().getNano());
}
default boolean isBefore(ChronoZonedDateTime<?> other) {
long thisEpochSec = toEpochSecond();
long otherEpochSec = other.toEpochSecond();
return thisEpochSec < otherEpochSec ||
(thisEpochSec == otherEpochSec && toLocalTime().getNano() < other.toLocalTime().getNano());
}
default boolean isEqual(ChronoZonedDateTime<?> other) {
return toEpochSecond() == other.toEpochSecond() &&
toLocalTime().getNano() == other.toLocalTime().getNano();
}
来自 java.time.chrono.ChronoZonedDateTime;
阅读条件,
(isBefore(x) or isEquals(x)) == !isAfter(x)
如果你是 before
或 equal
你就不是 after
。
示例:
如果你在第二名或之后,你就不能成为第一名。
如果两个条件都为真,则意味着您是 before
、equal
或 after
。因为你不可能是 after
和 before
(除非你是盒子里的猫),所以你是 equal
.
代码解释
这些条件很简单,首先它会尝试将以秒为单位的时间与toEpochSecond()
进行比较。
然后,根据方法的不同,它会检查纳秒值以更精确。这部分将取决于方法。让我们来一个:
default boolean isAfter(ChronoZonedDateTime<?> other) {
long thisEpochSec = toEpochSecond();
long otherEpochSec = other.toEpochSecond();
return thisEpochSec > otherEpochSec ||
(thisEpochSec == otherEpochSec && toLocalTime().getNano() > other.toLocalTime().getNano());
}
要检查 this
是否在 other
之后,我们需要检查 this.seconds
是否大于 other.seconds
。
thisEpochSec > otherEpochSec
如果不是,它会更深入,只有在秒数相等时才会检查到纳秒
thisEpochSec == otherEpochSec // seconds equals
&& toLocalTime().getNano() > other.toLocalTime().getNano());
如果您的问题是您是否可以证明 isBefore
、isEqual
和 isAfter
中的一个是正确的——是的,您可以。
this.toEpochSecond()
和 other.toEpochSecond()
之间存在三种可能的比较:<
、==
或 >
。看逻辑,很明显,如果比较的是<
,那么isBefore()
为真,isAfter()
和isEqual()
为假,因为>
和==
对 toEpochSecond
的比较将是错误的。同样,如果比较是 >
,则 isAfter()
为真,而 isBefore()
和 isEqual()
为假。所以在这两种情况下,这个命题成立。
第三种情况呢,toEpochSecond
结果相等?然后我们比较 toLocalTime().getNano()
,它又可以是 <
、==
或 >
。如果我们查看代码在 toEpochSecond
结果相等和比较 getNano
结果时的行为方式,我们发现在所有三种情况下,只有一种方法 return 为真,而其他方法return 错误。
所以从本质上讲,我们已经研究了五种可能的情况,在所有情况下,这三种方法中只有一种是正确的。由于我们已经证明了这一点,因此您原来问题中的陈述是:
(isAfter(x) or isEquals(x)) == !isBefore(x)
(isBefore(x) or isEquals(x)) == !isAfter(x)
也是如此。 QED