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