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.
