Java 使用 JodaTime 检查日期 X 是否在日期 Y 之后
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18532243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if Date X is after Date Y with JodaTime
提问by Mike Baxter
I'm using the jodatime DateTime
variable. I'd like to check if DateTime 'x' is after DateTime 'y'. The isAfter function only accepts a long
parameter, not a DateTime
, which I find very odd. Whats the best way to compare two DateTime
s?
我正在使用 jodatimeDateTime
变量。我想检查 DateTime 'x' 是否在 DateTime 'y' 之后。isAfter 函数只接受一个long
参数,而不是 a DateTime
,我觉得这很奇怪。比较两个DateTime
s的最佳方法是什么?
采纳答案by piet.t
There is a Method boolean isAfter(ReadableInstant instant)
in ReadableInstant, so as DateTime implements ReadableInstant it should accept a DateTime as well.
boolean isAfter(ReadableInstant instant)
ReadableInstant 中有一个方法,因此当 DateTime 实现 ReadableInstant 时,它也应该接受 DateTime。
回答by u6f6o
public static void main(String[] args) {
System.err.println(new DateTime().isAfter(new DateTime().plusDays(1)));
}
works for me
为我工作
回答by Darryl Gerrow
There is also a getMillis()
method inherited from BaseDateTime
that gives you a long
value representing the dates, you can compare the two long
values to determine which is before, and by how much.
还有一个getMillis()
继承自的方法,BaseDateTime
它为您提供一个long
表示日期的值,您可以比较这两个long
值以确定哪个在之前,以及相差多少。
回答by Prateek
Method isAfter Description
方法在描述之后
public boolean isAfter(ReadableInstant instant)
Is this instant after the instant passed in comparing solely by millisecond.
Specified by:
isAfter in interface ReadableInstant
Parameters:
instant - an instant to check against, null means now
Returns:
true if the instant is after the instant passed in
It takes ReadableInstant
as argument so DateTime
can also be passed as clear from DateTime Hierarchy
它需要ReadableInstant
作为参数,因此DateTime
也可以从DateTime Hierarchy
Class DateTime
java.lang.Object
extended by org.joda.time.base.AbstractInstant
extended by org.joda.time.base.AbstractDateTime
extended by org.joda.time.base.BaseDateTime
extended by org.joda.time.DateTime
All Implemented Interfaces:
Serializable, Comparable<ReadableInstant>, ReadableDateTime, ReadableInstant
Above details is been taken from joda api docs
以上细节取自joda api docs