Comparing Instant objects

Comparing two Instant objects can be accomplished via the  Instant.isAfter() and Instant.isBefore() methods. For example, let's look at the following two Instant objects:

Instant timestamp1 = Instant.now();
Instant timestamp2 = timestamp1.plusSeconds(10);

Check whether timestamp1 is after timestamp2:

boolean isAfter = timestamp1.isAfter(timestamp2); // false

Check whether timestamp1 is before timestamp2:

boolean isBefore = timestamp1.isBefore(timestamp2); // true

The time difference between two Instant objects can be computed via the Instant.until() method:

// 10 seconds
long difference = timestamp1.until(timestamp2, ChronoUnit.SECONDS);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset