Calculate Number of Weeks between Two Dates in Java

Learn to calculate the number of weeks between two given dates in Java. We will utilize the new Java 8 date-time API and its classes for calculation purposes. We will also learn to adjust the timezone offsets while doing the calculations.

Before diving into the implementation, let’s grasp the problem at hand. Calculating the number of weeks between two dates involves calculating the difference in days and then converting that difference into weeks. Additionally, we may need to consider the partial weeks i.e. if the difference in days is not perfectly divisible by 7.

1. Get Number of Weeks using ChronoUnit.WEEKS.between()

The ChronoUnit.WEEKS.between() method calculates the difference between two Temporal types such as LocalDate, LocalDateTime etc.

For example, when using LocalDate objects, we do not care about the timezone rules and, thus, the calculation becomes very easy.

LocalDate startLd = LocalDate.of(2023, 8, 5);
LocalDate endLd = LocalDate.of(2023, 9, 5);  // Difference is 31 days

long numWeeks = ChronoUnit.WEEKS.between(startLd, endLd);  //4

If we are interested in knowing the partial weeks as well, then we need to know the exact difference in days and calculate it ourselves.

long daysDifference = ChronoUnit.DAYS.between(startLd, endLd);  // 31
int extraDays = Long.valueOf(daysDifference % 7).intValue(); //3

2. Number of Weeks between Two ZonedDateTime Instances

By extending the previous example, we can calculate the difference in weeks between two dates in different timezones as well. In Java 8, ZonedDateTime class represents a date and time with a time zone in the ISO-8601 calendar system. This class accounts for daylight saving time and provides a complete solution for working with instants in time across various time zones.

The between() method internally calls the ZonedDateTime.until() method that calculates the amount of time between two ZonedDateTime objects. If the time-zone differs between the two zoned date-times, the specified end date-time is normalized to have the same zone as this date-time.

ZonedDateTime startDateTime = ZonedDateTime.parse("2022-01-01T00:00:00Z[UTC]");
ZonedDateTime endDateTime = ZonedDateTime.parse("2022-12-31T23:59:59Z[UTC]");

long numWeeks = ChronoUnit.WEEKS.between(startDateTime, endDateTime);  

The calculation effectively converts both zoned datetime objects to instants and then calculates the period between the instants.

3. Conclusion

In this Java tutorial, we have explored different approaches for calculating the number of weeks between two dates using the Java Date and Time API. Though the discussed concepts are simple, still useful when needed.

Happy Learning !!

Source Code on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.