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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:49:40  来源:igfitidea点击:

Check if Date X is after Date Y with JodaTime

javajodatime

提问by Mike Baxter

I'm using the jodatime DateTimevariable. I'd like to check if DateTime 'x' is after DateTime 'y'. The isAfter function only accepts a longparameter, not a DateTime, which I find very odd. Whats the best way to compare two DateTimes?

我正在使用 jodatimeDateTime变量。我想检查 DateTime 'x' 是否在 DateTime 'y' 之后。isAfter 函数只接受一个long参数,而不是 a DateTime,我觉得这很奇怪。比较两个DateTimes的最佳方法是什么?

采纳答案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 BaseDateTimethat gives you a longvalue representing the dates, you can compare the two longvalues 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 ReadableInstantas argument so DateTimecan 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