Signup/Sign In
LAST UPDATED: NOVEMBER 27, 2020

Java LocalDate until(ChronoLocalDate) Method

Java until() method is used to get the period between this date and another date as a Period. It calculates the period between two dates in terms of years, months, and days. The result will be negative if the end date is before the start date. The negative sign will be the same in each year, month and day.

It takes an argument of ChronoLocalDate type which can be any class object that implements it. The syntax of the method is given.

Syntax

public Period until(ChronoLocalDate endDateExclusive)

Parameters:

endDateExclusive - a date that represents the end date.

Returns:

It returns the period between this date and the end date.

Time for an Example:

Let's take an example to get a period between two dates. Here, we are using until() method to get a period between 2002-01-10 and 2005-10-12 dates.

import java.time.LocalDate;
import java.time.Period;
public class DateDemo {
	
	public static void main(String[] args){  
		
		LocalDate localDate = LocalDate.of(2002, 01, 10);
		System.out.println(localDate);
		Period period = localDate.until(LocalDate.of(2005,10,12));
		System.out.println("Total Periods : "+period);
	}
}


2002-01-10
Total Periods : P3Y9M2D

Time for another Example:

Let's take another example to understand the until() method. Here, we are getting a period of two dates and using getYears(), getMonths() and getDays() to get individual values of the period.

import java.time.LocalDate;
import java.time.Period;
public class DateDemo {
	
	public static void main(String[] args){  
		
		LocalDate localDate = LocalDate.of(2002, 01, 10);
		System.out.println(localDate);
		Period period = localDate.until(LocalDate.of(2005,10,12));
		System.out.println("Total Periods : "+period);
		System.out.println("Total Years : "+period.getYears());
		System.out.println("Total Months : "+period.getMonths());
		System.out.println("Total Days : "+period.getDays());
	}
}


2020-06-19
String date : 2020-06-19

Live Example:

Try with a live example, execute the code instantly with our powerful Online Java Compiler.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.