Hurry! Try our new Interactive Courses for FREE. 🥳   🚀
  Signup/Sign In

Java LocalDate until(Temporal, TemporalUnit) Method

Java until() method is used to get the amount of time until another date in terms of the specified unit. It calculates the amount of time between two LocalDate objects in terms of a single TemporalUnit. The result will be negative if the end date is before the start date. The Temporal passed to this method is converted to a LocalDate using from(TemporalAccessor) method.

For example, if we want to get the days between two LocalDates and then use the ChronoUnit enum value. The ChronoUnit is an enum that provides The units DAYS, WEEKS, MONTHS, YEARS, DECADES, CENTURIES, MILLENNIA and ERAS.

This method is similar to until(ChronoLocalDate) which returns the Period Between two localdate objects. We suggest you to read this method too.

It takes two arguments first is Temporal type and second is TemporalUnit type. The syntax of the method is given.

Syntax

public long until(Temporal endExclusive, TemporalUnit unit)

Parameters:

endExclusive - the end date, which is a LocalDate

unit - a unit to specify the date field.

Returns:

It returns a long type value.

Time for an Example:

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

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


2002-01-10
Years : 3

Time for another Example:

Let's take another example to understand the until() method. Here, we are getting days between two dates and using ChronoUnit.Days to get days of two dates.

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


2002-01-10
Days : 1371

Live Example:

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