Java Programs›Date & Time›Calculate Days Between Two Dates

Calculate Days Between Two Dates in Java

beginner·  Date & Time  ·  Date & Time

Problem

The difference between two dates can be measured as a single unit (a total number of days) or as a calendar period (months plus leftover days).

Find how many days and whole weeks lie between 15 January 2026 and 11 October 2026.

Input
2026-01-15, 2026-10-11
Output
269 days, 38 weeks

Java Program

Java
import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class DaysBetweenDates { public static void main(String[] args) { LocalDate start = LocalDate.of(2026, 1, 15); LocalDate end = LocalDate.of(2026, 10, 11); long days = ChronoUnit.DAYS.between(start, end); // end is exclusive long weeks = ChronoUnit.WEEKS.between(start, end); // whole weeks only long backwards = ChronoUnit.DAYS.between(end, start); System.out.println("Days between: " + days); System.out.println("Whole weeks: " + weeks); System.out.println("Reversed args: " + backwards); } }

Output

Days between: 269 Whole weeks: 38 Reversed args: -269

Core Logic

ChronoUnit.DAYS.between(start, end) counts the days from start up to, but not including, end.

How It Works
  1. 1LocalDate.of creates the two fixed dates.
  2. 2ChronoUnit.DAYS.between returns the total day count as a long.
  3. 3ChronoUnit.WEEKS.between counts only complete weeks.
  4. 4Swapping the arguments returns the same distance with a negative sign.
269 days is 38 weeks and 3 days, so the whole-week count is 38.
💡

Key Point: The end date is exclusive — DAYS.between(d, d.plusDays(1)) is 1, not 2; add one yourself if you need to count both endpoints, as in hotel or leave calculations.

Key Concepts

ChronoUnit.DAYS.between()ChronoUnit.WEEKSLocalDate.of()

Approach 2: Using Period.between()

Java
import java.time.LocalDate; import java.time.Period; public class DaysBetweenPeriod { public static void main(String[] args) { LocalDate start = LocalDate.of(2026, 1, 15); LocalDate end = LocalDate.of(2026, 10, 11); Period gap = Period.between(start, end); // calendar-aware: years, months and leftover days System.out.println(gap.getMonths() + " months and " + gap.getDays() + " days"); System.out.println("ISO form: " + gap); } }

Output

8 months and 26 days ISO form: P8M26D

Core Logic

Period expresses the gap the way people describe it — in years, months and leftover days — following the calendar.

How It Works
  1. 1Period.between(start, end) computes the calendar difference.
  2. 2getMonths() and getDays() return its parts.
  3. 3toString() prints the ISO form, such as P8M26D.
From 15 January, adding 8 months reaches 15 September, and 26 more days reaches 11 October.
💡

Key Point: Period.getDays() is only the leftover days (26), not the total (269) — use ChronoUnit.DAYS when you need the total.

Key Concepts

PeriodPeriod.between()ISO-8601 duration

Interview Questions

Why use ChronoUnit.DAYS.between instead of Period?

ChronoUnit.DAYS.between gives the total number of days. Period splits the gap into years, months and leftover days.

How do you count days including both the start and end?

Add one to the result, because between excludes the end date.

Learn the Concept

Related Programs