java.time.Instant.until() Method Example



Description

The java.time.Instant.until(Temporal endExclusive, TemporalUnit unit) method calculates the amount of time until another instant in terms of the specified unit.

Declaration

Following is the declaration for java.time.Instant.until(Temporal endExclusive, TemporalUnit unit) method.

public long until(Temporal endExclusive, TemporalUnit unit)

Parameters

  • endExclusive − the end date, exclusive, which is converted to an Instant, not null.

  • unit − the unit to measure the amount in, not null.

Return Value

the amount of time between this instant and the end instant.

Exceptions

  • DateTimeException − if the amount cannot be calculated, or the end temporal cannot be converted to an Instant.

  • UnsupportedTemporalTypeException − if the unit is not supported.

  • ArithmeticException − if numeric overflow occurs.

Example

The following example shows the usage of java.time.Instant.until(Temporal endExclusive, TemporalUnit unit) method.

package com.tutorialspoint;

import java.time.Instant;
import java.time.temporal.ChronoUnit;

public class InstantDemo {
   public static void main(String[] args) {

      Instant instant = Instant.parse("2017-12-03T10:15:30.00Z");
      Instant instant1 = Instant.parse("2017-12-03T10:18:30.00Z");
      System.out.println(instant.until(instant1, ChronoUnit.SECONDS));
   }
}

Let us compile and run the above program, this will produce the following result −

180
Advertisements