Java Calculate Number of Years Between two LocalDate
Tags: Java LocalDate ChronoUnit Java 8
In this Java core tutorial, we learn how to calculate the number of years between two LocalDate objects using the java.time.temporal.ChronoUnit class in Java programming language.
How to get number of years between two LocalDate in Java
In Java, we can use the ChronoUnit.YEARS.between() method to calculate the number of years between two LocalDate values as the following Java program.
YearsBetweenLocalDateExample.java
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class YearsBetweenLocalDateExample {
public static void main(String... args) {
LocalDate start = LocalDate.of(1996, 1, 1);
LocalDate end = LocalDate.of(2022, 1, 1);
// Calculate Number of Years Between two LocalDate
long numberOfYears = ChronoUnit.YEARS.between(start, end);
System.out.println(String.format("Number of years between %s and %s is %d", start, end, numberOfYears));
}
}
Number of years between 1996-01-01 and 2022-01-01 is 26
Happy Coding 😊
Related Articles
Java Calculate Number of Decades Between two LocalDate
Java Calculate Number of Months Between two LocalDate