我有两个JodaTime对象,我想要一个像这样的方法
// Return the latest of the two DateTimes
DateTime latest(DateTime a, DateTime b)
Run Code Online (Sandbox Code Playgroud)
但我找不到这样的事情.我可以轻松地写出来,但我确信JodaTime会把它放在某个地方.
小智 16
杰克指出,DateTime工具Comparable.如果您使用的是番石榴,最多可以通过以下简写确定两个日期(比如说a和b):
Ordering.natural().max(a, b);
Run Code Online (Sandbox Code Playgroud)
Jac*_*ack 15
DateTime实现Comparable所以除了执行以下操作之外,您不需要自己滚动:
DateTime latest(DateTime a, DateTime b)
{
return a.compareTo(b) > 0 ? a : b;
}
Run Code Online (Sandbox Code Playgroud)
或直接使用JodaTime API(考虑到Chronology不同compareTo):
DateTime latest(DateTime a, DateTime b)
{
return a.isAfter(b) ? a : b;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5801 次 |
| 最近记录: |