LocalDate until() Method in Java


The difference between two LocalDate objects can be obtained using the until() method in the LocalDate class in Java. This method requires a single parameter i.e. the end date for the LocalDate object and it returns the difference between two LocalDate objects using a Period object.

A program that demonstrates this is given as follows:

Example

 Live Demo

import java.time.*;
public class Demo {
   public static void main(String[] args) {
      LocalDate ld1 = LocalDate.parse("2019-01-10");
      LocalDate ld2 = LocalDate.parse("2019-02-14");
      System.out.println("The first LocalDate is: " + ld1);
      System.out.println("The second LocalDate is: " + ld2);
      System.out.println("The difference between two LocalDates is: " + ld1.until(ld2));
   }
}

Output

The first LocalDate is: 2019-01-10
The second LocalDate is: 2019-02-14
The difference between two LocalDates is: P1M4D

Now let us understand the above program.

First the two LocalDate objects are displayed. Then the difference between them is obtained using the until() method and displayed. A code snippet that demonstrates this is as follows:

LocalDate ld1 = LocalDate.parse("2019-01-10");
LocalDate ld2 = LocalDate.parse("2019-02-14");
System.out.println("The first LocalDate is: " + ld1);
System.out.println("The second LocalDate is: " + ld2);
System.out.println("The difference between two LocalDates is: " + ld1.until(ld2));

Updated on: 30-Jul-2019

70 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements