java 如何根据日期而不是时间比较两个 Instant

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27813691/
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-11-02 12:26:13  来源:igfitidea点击:

How to compare two Instant based on the date not time

java

提问by Xiaohe Dong

I want to have a simple solution to compare two Instant objects in Java. The comparison rule should be based on the date not time.

我想要一个简单的解决方案来比较 Java 中的两个 Instant 对象。比较规则应该基于日期而不是时间。

public boolean isAfterBasedOnDate(Instant instant, Instant compareTo) {
//TODO
}

For example,

例如,

  • instant: 2013-01-03T00:00:00Z
  • instant (compare to): 2013-01-03T15:00:00Z
  • isAfterBasedOnDate return false
  • 即时:2013-01-03T00:00:00Z
  • 即时(比较):2013-01-03T15:00:00Z
  • isAfterBasedOnDate 返回false


  • instant: 2013-01-03T15:00:00Z
  • instant (compare to): 2013-01-30T00:00:00Z
  • isAfterBasedOnDate return true
  • 即时:2013-01-03T15:00:00Z
  • 即时(比较):2013-01-30T00:00:00Z
  • isAfterBasedOnDate 返回true

Is there any simple way to do it ?

有什么简单的方法可以做到吗?

回答by Deepak Bala

Truncate the Instantto the number of days and then compare the truncated values.

将 截断Instant为天数,然后比较截断的值。

  public static void main(String[] args) {
    Instant now = Instant.now();
    System.out.println(now);
    Instant truncated = now.truncatedTo(ChronoUnit.DAYS);
    System.out.println(truncated);
  }
2015-01-07T06:43:30.679Z
2015-01-07T00:00:00Z
2015-01-07T06:43:30.679Z
2015-01-07T00:00:00Z

回答by wassgren

Use the truncatedTo-method on the Instantobject to only get the number of days.

在对象上使用truncatedTo方法Instant仅获取天数。

public boolean isAfterBasedOnDate(Instant instant, Instant compareTo) {
    return instant.truncatedTo(ChronoUnit.DAYS)
                  .isAfter(compareTo.truncatedTo(ChronoUnit.DAYS));
}

@Test
public void test() {
    Assert.assertFalse(isAfterBasedOnDate(
            Instant.parse("2013-01-03T00:00:00Z"),
            Instant.parse("2013-01-03T15:00:00Z")));

    Assert.assertFalse(isAfterBasedOnDate(
            Instant.parse("2013-01-03T15:00:00Z"),
            Instant.parse("2013-01-03T00:00:00Z")));

    Assert.assertFalse(isAfterBasedOnDate(
            Instant.parse("2013-01-02T15:00:00Z"),
            Instant.parse("2013-01-03T00:00:00Z")));

    Assert.assertTrue(isAfterBasedOnDate(
            Instant.parse("2013-01-04T15:00:00Z"),
            Instant.parse("2013-01-03T00:00:00Z")));
}

回答by lvoelk

while the accepted answer is correct users reading this question should rethink whether they should be using an instant in cases like this. LocalDate is the appropriate way to store and compare dates for which time is irrelevant. A Truncated instant works but it inherently still implies a timezone which would be irrelevant.

虽然接受的答案是正确的,但阅读此问题的用户应该重新考虑在这种情况下是否应该使用 Instant。LocalDate 是存储和比较与时间无关的日期的合适方法。截断的即时有效,但它本质上仍然暗示一个无关紧要的时区。

回答by user2104038

In a simple way, You can compare to Instant in java with second precision like that:

以一种简单的方式,您可以将 Java 中的 Instant 与第二精度进行比较,如下所示:

   public boolean isAfterBasedOnDate(Instant instant, Instant compareTo) {
       return instant.getEpochSecond() - compareTo.getEpochSecond() > 0;
   }

additionally if need nano precision can simply use method like that:

另外如果需要纳米精度可以简单地使用这样的方法:

public boolean isAfterBasedOnDate(Instant instant, Instant compareTo) {
        long secondDiff = instant.getEpochSecond() - compareTo.getEpochSecond();
        return secondDiff > 0
                || (secondDiff == 0 && instant.getNano() - compareTo.getNano() > 0);
    }