-2

i have this Date String 2020-07-26T20:08:27Z i want to perform date comparison.

so is there any framework/util for this operation.

String s1 = "2020-07-26T20:08:27Z";
String s2 = "2020-07-26T21:08:27Z";

in the above sample code, i want to find the bigger date

Naman
  • 21,685
  • 24
  • 196
  • 332
user2587669
  • 502
  • 4
  • 10
  • 20

2 Answers2

2

tl;dr

Instant
.parse( "2020-07-26T20:08:27Z" ) 
.isBefore(
    Instant.parse( "2020-07-26T21:08:27Z" )
)

true

java.time

Use the modern java.time classes built into Java 8 and later.

ISO 8601

The Z on the end of your input indicates an offset-from-UTC of zero hours-minutes-seconds, or UTC itself. The Z is pronounced “Zulu”.

Your input strings are in standard ISO 8601 format. The java.time classes use these formats by default when parsing/generating strings. So no need to specify a formatting pattern.

Instant

An Instant object represents a moment in UTC.

Instant instantA = Instant.parse( "2020-07-26T20:08:27Z" ) ;
Instant instantB = Instant.parse( "2020-07-26T21:08:27Z" ) ;

Compare using equals, isBefore, isAfter.

boolean aBeforeB = instantA.isBefore( instantB ) ;

Duration

You can capture the time elapsed between the two moments as a Duration object.

Duration d = Duration.between( instantA , instantB ) ;

You can ask the Duration object if it is zero or negative.


Table of all date-time types in Java, both modern and legacy

Basil Bourque
  • 262,936
  • 84
  • 758
  • 1,028
0

For this use compareTo() Method. I Have an example for you

      SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd");
  Date d1 = sdformat.parse("2019-04-15");
  Date d2 = sdformat.parse("2019-08-10");
  System.out.println("The date 1 is: " + sdformat.format(d1));
  System.out.println("The date 2 is: " + sdformat.format(d2));
  if(d1.compareTo(d2) > 0) {
     System.out.println("Date 1 occurs after Date 2");
  } else if(d1.compareTo(d2) < 0) {
     System.out.println("Date 1 occurs before Date 2");
  } else if(d1.compareTo(d2) == 0) {
     System.out.println("Both dates are equal");
  }
Tanzeem Iqbal
  • 105
  • 1
  • 6
  • 1
    FYI, the terribly flawed date-time classes such as [`java.util.Date`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html), `GregorianCalendar`, and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/tutorial/datetime/TOC.html) classes built into Java 8 and later. Suggesting their use in 2020 is poor advice. – Basil Bourque Jul 26 '20 at 21:46
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Jul 27 '20 at 00:13