Java Calculate Number of Days Between two LocalDate

Tags: Java LocalDate ChronoUnit Java 8

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

How to get number of days between LocalDate in Java

In Java, we can use the ChronoUnit.DAYS.between() method to calculate the number of days between two LocalDate values as the following Java program.

DaysBetweenLocalDateExample.java

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

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

        // Calculate Number of Days Between two LocalDate
        long numberOfDays = ChronoUnit.DAYS.between(start, end);

        System.out.println(String.format("Number of days between %s and %s is %d", start, end, numberOfDays));
    }
}
The output as below.
Number of days between 2022-08-01 and 2022-08-31 is 30

Happy Coding 😊

Java Calculate Number of Decades Between two LocalDate

Java Calculate Number of Years Between two LocalDate

Java Calculate Number of Months Between two LocalDate

Java Calculate Number of Weeks Between two LocalDate