Open In App

LocalDate until(ChronoLocalDate) Method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

until() method of the LocalDate class used to get the difference between this Local date and another date passed as a parameter and return the difference in term of Period object. This operation is performed in terms of years, months and days and return the answer in same. The start point is this LocalDate and the endpoint is the specified date passed as a parameter. when the start point is the after in term of date than the endpoint then the return value is negative.ISO calendar is very important for this method because the calculation is performed using the ISO calendar system.

Syntax: 

public Period until(ChronoLocalDate endDateExclusive)

Parameters: This method accepts one parameter endDateExclusive which is the end date, exclusive, which may be in any chronology, It should not be null.

Return value: This method returns the period between this date and the end date.

Below programs illustrate the until() method: 

Program 1:  

Java




// Java program to demonstrate
// LocalDate.until() method
 
import java.time.*;
import java.time.temporal.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // create LocalDate objects
        LocalDate l1
            = LocalDate
                  .parse("2018-12-06");
 
        LocalDate l2
            = LocalDate
                  .parse("2018-10-25");
 
        // apply until the method of LocalDate class
        Period result
            = l2.until(l1);
 
        // print results
        System.out.println("Result in Period: "
                           + result);
    }
}


Output: 

Result in Period: P1M11D

 

Program 2: 

Java




// Java program to demonstrate
// LocalDate.until() method
 
import java.time.*;
import java.time.temporal.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // create LocalDate objects
        LocalDate l1
            = LocalDate
                  .parse("2018-12-06");
 
        LocalDate l2
            = LocalDate
                  .parse("2018-12-15");
 
        // apply until()
        Period result
            = l2.until(l1);
 
        // print results
        System.out.println("Result in Period: "
                           + result);
    }
}


Output: 

Result in Period: P-9D

 

References: https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#until(java.time.chrono.ChronoLocalDate)
 



Last Updated : 24 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads