java.time.LocalDate.until() Method Example



Description

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

Declaration

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

public long until(Temporal endExclusive, TemporalUnit unit)

Parameters

  • endDateExclusive − the end date, exclusive, which is converted to a LocalDate, not null.

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

Return Value

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

Exceptions

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

  • UnsupportedTemporalTypeException − if the unit is not supported.

  • ArithmeticException − if numeric overflow occurs.

Example

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

package com.tutorialspoint;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class LocalDateDemo {
   public static void main(String[] args) {
	  LocalDate date = LocalDate.parse("2017-01-03");
      LocalDate date1 = LocalDate.now();
      System.out.println(date.until(date1, ChronoUnit.DAYS));  
   }
}

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

343
Advertisements