[英]Math.max(…) for JodaTime
I have two JodaTime objects and I wanted a method like so 我有两个JodaTime对象,我想要一个这样的方法
// Return the latest of the two DateTimes
DateTime latest(DateTime a, DateTime b)
But I can't find such a thing. 但我找不到这样的事情。 I could easily write it, but I'm sure JodaTime would have it somewhere.
我可以轻松地写出来,但我确信JodaTime会把它放在某个地方。
As Jack pointed out, DateTime
implements Comparable
. 正如杰克指出的那样,
DateTime
实现了Comparable
。 If you are using Guava, the maximum of two dates (let's say a
and b
) can be determined by the following shorthand: 如果您使用的是番石榴,最多两个日期(比如说
a
和b
)可以通过以下简写来确定:
Ordering.natural().max(a, b);
DateTime
implements Comparable
so you don't need to roll you own other than doing: DateTime
实现了Comparable
因此除了执行以下操作之外,您不需要自己滚动:
DateTime latest(DateTime a, DateTime b)
{
return a.compareTo(b) > 0 ? a : b;
}
or by using JodaTime API directly (which takes into account Chronology
unlike compareTo
): 或直接使用JodaTime API(与
compareTo
不同,它考虑了Chronology
):
DateTime latest(DateTime a, DateTime b)
{
return a.isAfter(b) ? a : b;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.