Java Calculate Number of Decades Between two LocalDateTime

Tags: Java LocalDateTime ChronoUnit Java 8

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

How to get number of decades between two LocalDateTime in Java

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

DecadesBetweenLocalDateTimeExample.java

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class DecadesBetweenLocalDateTimeExample {
    public static void main(String... args) {
        LocalDateTime start = LocalDateTime.parse("2000-01-01T08:30:00");
        LocalDateTime end = LocalDateTime.parse("2022-08-30T23:50:00");

        // Calculate Number of Decades Between two LocalDateTime
        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-01T08:30 and 2022-08-30T23:50 is 2

Happy Coding 😊

Java Calculate Number of Years Between two LocalDateTime

Java Calculate Number of Months Between two LocalDateTime

Java Calculate Number of Weeks Between two LocalDateTime

Java Calculate Number of Days Between two LocalDateTime

Java Calculate Number of Hours Between two LocalDateTime

Java Calculate Number of Minutes Between two LocalDateTime

Java Calculate Number of Seconds Between two LocalDateTime

Java Calculate Number of Milliseconds Between two LocalDateTime

Java Calculate Number of Nanoseconds Between two LocalDateTime