Java Calculate Number of Decades Between two LocalDate

Tags: Java LocalDate ChronoUnit Java 8

In this Java core tutorial, we learn how to calculate the number of decades between two LocalDate objects using the java.time.temporal.ChronoUnit class in Java programming language.

How to get number of decades between two LocalDate in Java

In Java, we can use the ChronoUnit.DECADES.between() method to calculate the number of decades between two LocalDate values as the following example Java code.

DecadesBetweenLocalDateExample.java

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class DecadesBetweenLocalDateExample {
    public static void main(String... args) {
        LocalDate start = LocalDate.of(2000, 1, 1);
        LocalDate end = LocalDate.of(2022, 1, 1);

        // Calculate Number of Decades Between two LocalDate
        long numberOfDecades = ChronoUnit.DECADES.between(start, end);

        System.out.println(String.format("Number of decades between %s and %s is %d", start, end, numberOfDecades));
    }
}
The output as below.
Number of decades between 2000-01-01 and 2022-01-01 is 2

Happy Coding 😊

Java Calculate Number of Years Between two LocalDate

Java Calculate Number of Months Between two LocalDate

Java Calculate Number of Weeks Between two LocalDate

Java Calculate Number of Days Between two LocalDate