Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two Instant based on the date not time

Tags:

java

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.

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

  • instant: 2013-01-03T15:00:00Z
  • instant (compare to): 2013-01-30T00:00:00Z
  • isAfterBasedOnDate return true

Is there any simple way to do it ?

like image 413
Xiaohe Dong Avatar asked Jan 07 '15 06:01

Xiaohe Dong


People also ask

How do you compare two dates without the time portion?

If you want to compare just the date part without considering time, you need to use DateFormat class to format the date into some format and then compare their String value. Alternatively, you can use joda-time which provides a class LocalDate, which represents a Date without time, similar to Java 8's LocalDate class.

How do you compare two instants?

Two Instant objects can be compared using the compareTo() method in the Instant class in Java. This method requires a single parameter i.e. the Instant object to be compared.

How do you compare two date values?

In Java, two dates can be compared using the compareTo() method of Comparable interface. This method returns '0' if both the dates are equal, it returns a value "greater than 0" if date1 is after date2 and it returns a value "less than 0" if date1 is before date2.

How do you compare two strings in dates?

To compare two date strings:Pass the strings to the Date() constructor to create 2 Date objects. Compare the output from calling the getTime() method on the dates.


3 Answers

Truncate the Instant to the number of days and then compare the truncated values.

  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
like image 130
Deepak Bala Avatar answered Sep 25 '22 01:09

Deepak Bala


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

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")));
}
like image 32
wassgren Avatar answered Sep 26 '22 01:09

wassgren


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.

like image 26
lvoelk Avatar answered Sep 25 '22 01:09

lvoelk