When calculating ages, finding days until deadlines, or scheduling future events, you need date arithmetic. Java provides Period for date-based amounts and Duration for time-based amounts, making these calculations precise and readable.

DateTime calculations perform arithmetic with dates and times. Java uses Period for date-based amounts and Duration for time-based amounts.

Date Arithmetic

today
Arithmetic.java
Replay: real traced execution (multi-file project)
// Add and subtract dates

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

public class Arithmetic {

    public static void main(String[] args) {
        LocalDate today = LocalDate.of(2025, 1, 29);
        System.out.println("Today: " + today);
        System.out.println();

        // Add days
        System.out.println("Add days:");
        System.out.println("Tomorrow: " + today.plusDays(1));
        System.out.println("+7 days: " + today.plusDays(7));
        System.out.println("+30 days: " + today.plusDays(30));

        // Add weeks
        System.out.println("\nAdd weeks:");
        System.out.println("+1 week: " + today.plusWeeks(1));
        System.out.println("+4 weeks: " + today.plusWeeks(4));

        // Add months
        System.out.println("\nAdd months:");
        System.out.println("+1 month: " + today.plusMonths(1));
        System.out.println("+3 months: " + today.plusMonths(3));
        System.out.println("+12 months: " + today.plusMonths(12));

        // Add years
        System.out.println("\nAdd years:");
        System.out.println("+1 year: " + today.plusYears(1));
        System.out.println("+5 years: " + today.plusYears(5));

        // Subtract
        System.out.println("\nSubtract:");
        System.out.println("Yesterday: " + today.minusDays(1));
        System.out.println("-1 week: " + today.minusWeeks(1));
        System.out.println("-1 month: " + today.minusMonths(1));
        System.out.println("-1 year: " + today.minusYears(1));

        // Chaining
        System.out.println("\nChaining:");
        LocalDate future = today.plusMonths(3).plusDays(15);
        System.out.println("+3 months +15 days: " + future);

        // Period
        System.out.println("\nUsing Period:");
        Period p1 = Period.ofDays(7);
        System.out.println("Period of 7 days: " + today.plus(p1));

        Period p2 = Period.ofMonths(2);
        System.out.println("Period of 2 months: " + today.plus(p2));

        Period p3 = Period.of(1, 3, 15);  // 1 year, 3 months, 15 days
        System.out.println("Period 1y 3m 15d: " + today.plus(p3));

        // DateTime arithmetic
        System.out.println("\nDateTime arithmetic:");
        LocalDateTime now = LocalDateTime.of(2025, 1, 29, 14, 30, 0);
        System.out.println("Now: " + now);
        System.out.println("+2 hours: " + now.plusHours(2));
        System.out.println("+30 minutes: " + now.plusMinutes(30));
        System.out.println("+1 day 2 hours: " + now.plusDays(1).plusHours(2));

        // End of periods
        System.out.println("\nEnd of periods:");
        System.out.println("End of week: " + today.plusWeeks(1));
        System.out.println("End of month: " + today.plusMonths(1));
        System.out.println("End of quarter: " + today.plusMonths(3));
        System.out.println("End of year: " + today.plusYears(1));

        // Practical examples
        System.out.println("\nPractical examples:");
        System.out.println("30-day trial ends: " + today.plusDays(30));
        System.out.println("Quarterly review: " + today.plusMonths(3));
        System.out.println("Annual renewal: " + today.plusYears(1));
        System.out.println("Two weeks notice: " + today.plusWeeks(2));
    }

}
// Add and subtract dates

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

public class Arithmetic {

    public static void main(String[] args) {
        LocalDate today = LocalDate.of(2024, 2, 28);
        System.out.println("Today: " + today);
        System.out.println();

        // Add days
        System.out.println("Add days:");
        System.out.println("Tomorrow: " + today.plusDays(1));
        System.out.println("+7 days: " + today.plusDays(7));
        System.out.println("+30 days: " + today.plusDays(30));

        // Add weeks
        System.out.println("\nAdd weeks:");
        System.out.println("+1 week: " + today.plusWeeks(1));
        System.out.println("+4 weeks: " + today.plusWeeks(4));

        // Add months
        System.out.println("\nAdd months:");
        System.out.println("+1 month: " + today.plusMonths(1));
        System.out.println("+3 months: " + today.plusMonths(3));
        System.out.println("+12 months: " + today.plusMonths(12));

        // Add years
        System.out.println("\nAdd years:");
        System.out.println("+1 year: " + today.plusYears(1));
        System.out.println("+5 years: " + today.plusYears(5));

        // Subtract
        System.out.println("\nSubtract:");
        System.out.println("Yesterday: " + today.minusDays(1));
        System.out.println("-1 week: " + today.minusWeeks(1));
        System.out.println("-1 month: " + today.minusMonths(1));
        System.out.println("-1 year: " + today.minusYears(1));

        // Chaining
        System.out.println("\nChaining:");
        LocalDate future = today.plusMonths(3).plusDays(15);
        System.out.println("+3 months +15 days: " + future);

        // Period
        System.out.println("\nUsing Period:");
        Period p1 = Period.ofDays(7);
        System.out.println("Period of 7 days: " + today.plus(p1));

        Period p2 = Period.ofMonths(2);
        System.out.println("Period of 2 months: " + today.plus(p2));

        Period p3 = Period.of(1, 3, 15);  // 1 year, 3 months, 15 days
        System.out.println("Period 1y 3m 15d: " + today.plus(p3));

        // DateTime arithmetic
        System.out.println("\nDateTime arithmetic:");
        LocalDateTime now = LocalDateTime.of(2025, 1, 29, 14, 30, 0);
        System.out.println("Now: " + now);
        System.out.println("+2 hours: " + now.plusHours(2));
        System.out.println("+30 minutes: " + now.plusMinutes(30));
        System.out.println("+1 day 2 hours: " + now.plusDays(1).plusHours(2));

        // End of periods
        System.out.println("\nEnd of periods:");
        System.out.println("End of week: " + today.plusWeeks(1));
        System.out.println("End of month: " + today.plusMonths(1));
        System.out.println("End of quarter: " + today.plusMonths(3));
        System.out.println("End of year: " + today.plusYears(1));

        // Practical examples
        System.out.println("\nPractical examples:");
        System.out.println("30-day trial ends: " + today.plusDays(30));
        System.out.println("Quarterly review: " + today.plusMonths(3));
        System.out.println("Annual renewal: " + today.plusYears(1));
        System.out.println("Two weeks notice: " + today.plusWeeks(2));
    }

}
  1. today ← 2025-01-29, future ← 2025-05-14, p1 ← P7D, p2 ← P2M, p3 ← P1Y3M15D

    10public static void main(String[] args) {11    LocalDate today→ 2025-01-29 = LocalDate.of(2025, 1, 29); //@today=LocalDate.of(2025, 1, 29), LocalDate.of(2024, 2, 28)12    System.out.println("Today: " + today2025-01-29);13    System.out.println();1415    // Add days16    System.out.println("Add days:");17    System.out.println("Tomorrow: " + today.plusDays(1));18    System.out.println("+7 days: " + today.plusDays(7));19    System.out.println("+30 days: " + today.plusDays(30));2021    // Add weeks22    System.out.println("\nAdd weeks:");23    System.out.println("+1 week: " + today.plusWeeks(1));24    System.out.println("+4 weeks: " + today.plusWeeks(4));2526    // Add months27    System.out.println("\nAdd months:");28    System.out.println("+1 month: " + today.plusMonths(1));29    System.out.println("+3 months: " + today.plusMonths(3));30    System.out.println("+12 months: " + today.plusMonths(12));3132    // Add years33    System.out.println("\nAdd years:");34    System.out.println("+1 year: " + today.plusYears(1));35    System.out.println("+5 years: " + today.plusYears(5));3637    // Subtract38    System.out.println("\nSubtract:");39    System.out.println("Yesterday: " + today.minusDays(1));40    System.out.println("-1 week: " + today.minusWeeks(1));41    System.out.println("-1 month: " + today.minusMonths(1));42    System.out.println("-1 year: " + today.minusYears(1));4344    // Chaining45    System.out.println("\nChaining:");46    LocalDate future→ 2025-05-14 = today.plusMonths(3).plusDays(15);47    System.out.println("+3 months +15 days: " + future2025-05-14);4849    // Period50    System.out.println("\nUsing Period:");51    Period p1→ P7D = Period.ofDays(7);52    System.out.println("Period of 7 days: " + today.plus(p1P7D));53    54    Period p2→ P2M = Period.ofMonths(2);55    System.out.println("Period of 2 months: " + today.plus(p2P2M));56    57    Period p3→ P1Y3M15D = Period.of(1, 3, 15);  // 1 year, 3 months, 15 days58    System.out.println("Period 1y 3m 15d: " + today.plus(p3P1Y3M15D));5960    // DateTime arithmetic61    System.out.println("\nDateTime arithmetic:");62    LocalDateTime now→ 2025-01-29T14:30 = LocalDateTime.of(2025, 1, 29, 14, 30, 0);63    System.out.println("Now: " + now2025-01-29T14:30);64    System.out.println("+2 hours: " + now.plusHours(2));65    System.out.println("+30 minutes: " + now.plusMinutes(30));66    System.out.println("+1 day 2 hours: " + now.plusDays(1).plusHours(2));6768    // End of periods69    System.out.println("\nEnd of periods:");70    System.out.println("End of week: " + today.plusWeeks(1));71    System.out.println("End of month: " + today.plusMonths(1));72    System.out.println("End of quarter: " + today.plusMonths(3));73    System.out.println("End of year: " + today.plusYears(1));7475    // Practical examples76    System.out.println("\nPractical examples:");77    System.out.println("30-day trial ends: " + today.plusDays(30));78    System.out.println("Quarterly review: " + today.plusMonths(3));79    System.out.println("Annual renewal: " + today.plusYears(1));80    System.out.println("Two weeks notice: " + today.plusWeeks(2));81}
    outputToday: 2025-01-29
    Add days:
    Tomorrow: 2025-01-30
    +7 days: 2025-02-05
    +30 days: 2025-02-28
    
    Add weeks:
    +1 week: 2025-02-05
    +4 weeks: 2025-02-26
    
    Add months:
    +1 month: 2025-02-28
    +3 months: 2025-04-29
    +12 months: 2026-01-29
    
    Add years:
    +1 year: 2026-01-29
    +5 years: 2030-01-29
    
    Subtract:
    Yesterday: 2025-01-28
    -1 week: 2025-01-22
    -1 month: 2024-12-29
    -1 year: 2024-01-29
    
    Chaining:
    +3 months +15 days: 2025-05-14
    
    Using Period:
    Period of 7 days: 2025-02-05
    Period of 2 months: 2025-03-29
    Period 1y 3m 15d: 2026-05-14
    
    DateTime arithmetic:
    Now: 2025-01-29T14:30
    +2 hours: 2025-01-29T16:30
    +30 minutes: 2025-01-29T15:00
    +1 day 2 hours: 2025-01-30T16:30
    
    End of periods:
    End of week: 2025-02-05
    End of month: 2025-02-28
    End of quarter: 2025-04-29
    End of year: 2026-01-29
    
    Practical examples:
    30-day trial ends: 2025-02-28
    Quarterly review: 2025-04-29
    Annual renewal: 2026-01-29
    Two weeks notice: 2025-02-12
  1. today ← 2024-02-28, future ← 2024-06-12, p1 ← P7D, p2 ← P2M, p3 ← P1Y3M15D

    10public static void main(String[] args) {11    LocalDate today→ 2024-02-28 = LocalDate.of(2024, 2, 28);12    System.out.println("Today: " + today2024-02-28);13    System.out.println();1415    // Add days16    System.out.println("Add days:");17    System.out.println("Tomorrow: " + today.plusDays(1));18    System.out.println("+7 days: " + today.plusDays(7));19    System.out.println("+30 days: " + today.plusDays(30));2021    // Add weeks22    System.out.println("\nAdd weeks:");23    System.out.println("+1 week: " + today.plusWeeks(1));24    System.out.println("+4 weeks: " + today.plusWeeks(4));2526    // Add months27    System.out.println("\nAdd months:");28    System.out.println("+1 month: " + today.plusMonths(1));29    System.out.println("+3 months: " + today.plusMonths(3));30    System.out.println("+12 months: " + today.plusMonths(12));3132    // Add years33    System.out.println("\nAdd years:");34    System.out.println("+1 year: " + today.plusYears(1));35    System.out.println("+5 years: " + today.plusYears(5));3637    // Subtract38    System.out.println("\nSubtract:");39    System.out.println("Yesterday: " + today.minusDays(1));40    System.out.println("-1 week: " + today.minusWeeks(1));41    System.out.println("-1 month: " + today.minusMonths(1));42    System.out.println("-1 year: " + today.minusYears(1));4344    // Chaining45    System.out.println("\nChaining:");46    LocalDate future→ 2024-06-12 = today.plusMonths(3).plusDays(15);47    System.out.println("+3 months +15 days: " + future2024-06-12);4849    // Period50    System.out.println("\nUsing Period:");51    Period p1→ P7D = Period.ofDays(7);52    System.out.println("Period of 7 days: " + today.plus(p1P7D));53    54    Period p2→ P2M = Period.ofMonths(2);55    System.out.println("Period of 2 months: " + today.plus(p2P2M));56    57    Period p3→ P1Y3M15D = Period.of(1, 3, 15);  // 1 year, 3 months, 15 days58    System.out.println("Period 1y 3m 15d: " + today.plus(p3P1Y3M15D));5960    // DateTime arithmetic61    System.out.println("\nDateTime arithmetic:");62    LocalDateTime now→ 2025-01-29T14:30 = LocalDateTime.of(2025, 1, 29, 14, 30, 0);63    System.out.println("Now: " + now2025-01-29T14:30);64    System.out.println("+2 hours: " + now.plusHours(2));65    System.out.println("+30 minutes: " + now.plusMinutes(30));66    System.out.println("+1 day 2 hours: " + now.plusDays(1).plusHours(2));6768    // End of periods69    System.out.println("\nEnd of periods:");70    System.out.println("End of week: " + today.plusWeeks(1));71    System.out.println("End of month: " + today.plusMonths(1));72    System.out.println("End of quarter: " + today.plusMonths(3));73    System.out.println("End of year: " + today.plusYears(1));7475    // Practical examples76    System.out.println("\nPractical examples:");77    System.out.println("30-day trial ends: " + today.plusDays(30));78    System.out.println("Quarterly review: " + today.plusMonths(3));79    System.out.println("Annual renewal: " + today.plusYears(1));80    System.out.println("Two weeks notice: " + today.plusWeeks(2));81}
    outputToday: 2024-02-28
    Add days:
    Tomorrow: 2024-02-29
    +7 days: 2024-03-06
    +30 days: 2024-03-29
    
    Add weeks:
    +1 week: 2024-03-06
    +4 weeks: 2024-03-27
    
    Add months:
    +1 month: 2024-03-28
    +3 months: 2024-05-28
    +12 months: 2025-02-28
    
    Add years:
    +1 year: 2025-02-28
    +5 years: 2029-02-28
    
    Subtract:
    Yesterday: 2024-02-27
    -1 week: 2024-02-21
    -1 month: 2024-01-28
    -1 year: 2023-02-28
    
    Chaining:
    +3 months +15 days: 2024-06-12
    
    Using Period:
    Period of 7 days: 2024-03-06
    Period of 2 months: 2024-04-28
    Period 1y 3m 15d: 2025-06-12
    
    DateTime arithmetic:
    Now: 2025-01-29T14:30
    +2 hours: 2025-01-29T16:30
    +30 minutes: 2025-01-29T15:00
    +1 day 2 hours: 2025-01-30T16:30
    
    End of periods:
    End of week: 2024-03-06
    End of month: 2024-03-28
    End of quarter: 2024-05-28
    End of year: 2025-02-28
    
    Practical examples:
    30-day trial ends: 2024-03-29
    Quarterly review: 2024-05-28
    Annual renewal: 2025-02-28
    Two weeks notice: 2024-03-13
Period Represents a date-based amount of time in years, months, and days. Used for calendar calculations.

Calculating Differences

end
Difference.java
Replay: real traced execution (multi-file project)
// Calculate differences

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.Duration;
import java.time.temporal.ChronoUnit;

public class Difference {

    public static void main(String[] args) {
        LocalDate start = LocalDate.of(2025, 1, 1);
        LocalDate end = LocalDate.of(2025, 12, 31);

        System.out.println("Start: " + start);
        System.out.println("End: " + end);
        System.out.println();

        // Period between
        Period period = Period.between(start, end);
        System.out.println("Period between:");
        System.out.println("Years: " + period.getYears());
        System.out.println("Months: " + period.getMonths());
        System.out.println("Days: " + period.getDays());
        System.out.println("Full period: " + period);

        // Total days
        long days = ChronoUnit.DAYS.between(start, end);
        System.out.println("\nTotal days: " + days);

        // Other units
        System.out.println("\nOther units:");
        System.out.println("Weeks: " + ChronoUnit.WEEKS.between(start, end));
        System.out.println("Months: " + ChronoUnit.MONTHS.between(start, end));
        System.out.println("Years: " + ChronoUnit.YEARS.between(start, end));

        // Different date pairs
        System.out.println("\nDifferent pairs:");
        LocalDate d1 = LocalDate.of(2020, 1, 15);
        LocalDate d2 = LocalDate.of(2025, 3, 20);

        Period p = Period.between(d1, d2);
        System.out.println(d1 + " to " + d2);
        System.out.println("Period: " + p.getYears() + "y " + p.getMonths() + "m " + p.getDays() + "d");
        System.out.println("Total days: " + ChronoUnit.DAYS.between(d1, d2));

        // DateTime differences
        System.out.println("\nDateTime differences:");
        LocalDateTime dt1 = LocalDateTime.of(2025, 1, 29, 10, 0);
        LocalDateTime dt2 = LocalDateTime.of(2025, 1, 29, 15, 30);

        Duration duration = Duration.between(dt1, dt2);
        System.out.println(dt1 + " to " + dt2);
        System.out.println("Duration: " + duration);
        System.out.println("Hours: " + duration.toHours());
        System.out.println("Minutes: " + duration.toMinutes());
        System.out.println("Seconds: " + duration.toSeconds());

        // Time units
        System.out.println("\nTime units between:");
        System.out.println("Hours: " + ChronoUnit.HOURS.between(dt1, dt2));
        System.out.println("Minutes: " + ChronoUnit.MINUTES.between(dt1, dt2));
        System.out.println("Seconds: " + ChronoUnit.SECONDS.between(dt1, dt2));

        // Absolute difference
        System.out.println("\nAbsolute difference:");
        LocalDate past = LocalDate.of(2020, 1, 1);
        LocalDate future = LocalDate.of(2030, 1, 1);

        System.out.println("Past to future: " + ChronoUnit.DAYS.between(past, future));
        System.out.println("Future to past: " + ChronoUnit.DAYS.between(future, past));
        System.out.println("Absolute: " + Math.abs(ChronoUnit.DAYS.between(future, past)));

        // Practical examples
        System.out.println("\nPractical examples:");

        LocalDate birthday = LocalDate.of(1990, 3, 15);
        LocalDate today = LocalDate.of(2025, 1, 29);
        long age = ChronoUnit.YEARS.between(birthday, today);
        System.out.println("Age: " + age + " years");

        LocalDate projectStart = LocalDate.of(2025, 1, 1);
        LocalDate projectEnd = LocalDate.of(2025, 3, 31);
        long projectDays = ChronoUnit.DAYS.between(projectStart, projectEnd);
        System.out.println("Project duration: " + projectDays + " days");

        LocalDate subscriptionStart = LocalDate.of(2024, 1, 1);
        LocalDate subscriptionEnd = LocalDate.of(2025, 1, 1);
        long subscriptionMonths = ChronoUnit.MONTHS.between(subscriptionStart, subscriptionEnd);
        System.out.println("Subscription: " + subscriptionMonths + " months");
    }

}
// Calculate differences

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.Duration;
import java.time.temporal.ChronoUnit;

public class Difference {

    public static void main(String[] args) {
        LocalDate start = LocalDate.of(2025, 1, 1);
        LocalDate end = LocalDate.of(2025, 2, 1);

        System.out.println("Start: " + start);
        System.out.println("End: " + end);
        System.out.println();

        // Period between
        Period period = Period.between(start, end);
        System.out.println("Period between:");
        System.out.println("Years: " + period.getYears());
        System.out.println("Months: " + period.getMonths());
        System.out.println("Days: " + period.getDays());
        System.out.println("Full period: " + period);

        // Total days
        long days = ChronoUnit.DAYS.between(start, end);
        System.out.println("\nTotal days: " + days);

        // Other units
        System.out.println("\nOther units:");
        System.out.println("Weeks: " + ChronoUnit.WEEKS.between(start, end));
        System.out.println("Months: " + ChronoUnit.MONTHS.between(start, end));
        System.out.println("Years: " + ChronoUnit.YEARS.between(start, end));

        // Different date pairs
        System.out.println("\nDifferent pairs:");
        LocalDate d1 = LocalDate.of(2020, 1, 15);
        LocalDate d2 = LocalDate.of(2025, 3, 20);

        Period p = Period.between(d1, d2);
        System.out.println(d1 + " to " + d2);
        System.out.println("Period: " + p.getYears() + "y " + p.getMonths() + "m " + p.getDays() + "d");
        System.out.println("Total days: " + ChronoUnit.DAYS.between(d1, d2));

        // DateTime differences
        System.out.println("\nDateTime differences:");
        LocalDateTime dt1 = LocalDateTime.of(2025, 1, 29, 10, 0);
        LocalDateTime dt2 = LocalDateTime.of(2025, 1, 29, 15, 30);

        Duration duration = Duration.between(dt1, dt2);
        System.out.println(dt1 + " to " + dt2);
        System.out.println("Duration: " + duration);
        System.out.println("Hours: " + duration.toHours());
        System.out.println("Minutes: " + duration.toMinutes());
        System.out.println("Seconds: " + duration.toSeconds());

        // Time units
        System.out.println("\nTime units between:");
        System.out.println("Hours: " + ChronoUnit.HOURS.between(dt1, dt2));
        System.out.println("Minutes: " + ChronoUnit.MINUTES.between(dt1, dt2));
        System.out.println("Seconds: " + ChronoUnit.SECONDS.between(dt1, dt2));

        // Absolute difference
        System.out.println("\nAbsolute difference:");
        LocalDate past = LocalDate.of(2020, 1, 1);
        LocalDate future = LocalDate.of(2030, 1, 1);

        System.out.println("Past to future: " + ChronoUnit.DAYS.between(past, future));
        System.out.println("Future to past: " + ChronoUnit.DAYS.between(future, past));
        System.out.println("Absolute: " + Math.abs(ChronoUnit.DAYS.between(future, past)));

        // Practical examples
        System.out.println("\nPractical examples:");

        LocalDate birthday = LocalDate.of(1990, 3, 15);
        LocalDate today = LocalDate.of(2025, 1, 29);
        long age = ChronoUnit.YEARS.between(birthday, today);
        System.out.println("Age: " + age + " years");

        LocalDate projectStart = LocalDate.of(2025, 1, 1);
        LocalDate projectEnd = LocalDate.of(2025, 3, 31);
        long projectDays = ChronoUnit.DAYS.between(projectStart, projectEnd);
        System.out.println("Project duration: " + projectDays + " days");

        LocalDate subscriptionStart = LocalDate.of(2024, 1, 1);
        LocalDate subscriptionEnd = LocalDate.of(2025, 1, 1);
        long subscriptionMonths = ChronoUnit.MONTHS.between(subscriptionStart, subscriptionEnd);
        System.out.println("Subscription: " + subscriptionMonths + " months");
    }

}
  1. start ← 2025-01-01, end ← 2025-12-31, period ← P11M30D, days ← 364

    11public static void main(String[] args) {12    LocalDate start→ 2025-01-01 = LocalDate.of(2025, 1, 1);13    LocalDate end→ 2025-12-31 = LocalDate.of(2025, 12, 31); //@end=LocalDate.of(2025, 12, 31), LocalDate.of(2025, 2, 1)14    15    System.out.println("Start: " + start2025-01-01);16    System.out.println("End: " + end2025-12-31);17    System.out.println();1819    // Period between20    Period period→ P11M30D = Period.between(start2025-01-01, end2025-12-31);21    System.out.println("Period between:");22    System.out.println("Years: " + period.getYears());23    System.out.println("Months: " + period.getMonths());24    System.out.println("Days: " + period.getDays());25    System.out.println("Full period: " + periodP11M30D);2627    // Total days28    long days→ 364 = ChronoUnit.DAYS.between(start2025-01-01, end2025-12-31);29    System.out.println("\nTotal days: " + days364);3031    // Other units32    System.out.println("\nOther units:");33    System.out.println("Weeks: " + ChronoUnit.WEEKS.between(start2025-01-01, end2025-12-31));34    System.out.println("Months: " + ChronoUnit.MONTHS.between(start2025-01-01, end2025-12-31));35    System.out.println("Years: " + ChronoUnit.YEARS.between(start2025-01-01, end2025-12-31));3637    // Different date pairs38    System.out.println("\nDifferent pairs:");39    LocalDate d1→ 2020-01-15 = LocalDate.of(2020, 1, 15);40    LocalDate d2→ 2025-03-20 = LocalDate.of(2025, 3, 20);41    42    Period p→ P5Y2M5D = Period.between(d12020-01-15, d22025-03-20);43    System.out.println(d12020-01-15 + " to " + d22025-03-20);44    System.out.println("Period: " + p.getYears() + "y " + p.getMonths() + "m " + p.getDays() + "d");45    System.out.println("Total days: " + ChronoUnit.DAYS.between(d12020-01-15, d22025-03-20));4647    // DateTime differences48    System.out.println("\nDateTime differences:");49    LocalDateTime dt1→ 2025-01-29T10:00 = LocalDateTime.of(2025, 1, 29, 10, 0);50    LocalDateTime dt2→ 2025-01-29T15:30 = LocalDateTime.of(2025, 1, 29, 15, 30);51    52    Duration duration→ PT5H30M = Duration.between(dt12025-01-29T10:00, dt22025-01-29T15:30);53    System.out.println(dt12025-01-29T10:00 + " to " + dt22025-01-29T15:30);54    System.out.println("Duration: " + durationPT5H30M);55    System.out.println("Hours: " + duration.toHours());56    System.out.println("Minutes: " + duration.toMinutes());57    System.out.println("Seconds: " + duration.toSeconds());5859    // Time units60    System.out.println("\nTime units between:");61    System.out.println("Hours: " + ChronoUnit.HOURS.between(dt12025-01-29T10:00, dt22025-01-29T15:30));62    System.out.println("Minutes: " + ChronoUnit.MINUTES.between(dt12025-01-29T10:00, dt22025-01-29T15:30));63    System.out.println("Seconds: " + ChronoUnit.SECONDS.between(dt12025-01-29T10:00, dt22025-01-29T15:30));6465    // Absolute difference66    System.out.println("\nAbsolute difference:");67    LocalDate past→ 2020-01-01 = LocalDate.of(2020, 1, 1);68    LocalDate future→ 2030-01-01 = LocalDate.of(2030, 1, 1);69    70    System.out.println("Past to future: " + ChronoUnit.DAYS.between(past2020-01-01, future2030-01-01));71    System.out.println("Future to past: " + ChronoUnit.DAYS.between(future2030-01-01, past2020-01-01));72    System.out.println("Absolute: " + Math.abs(ChronoUnit.DAYS.between(future2030-01-01, past2020-01-01)));7374    // Practical examples75    System.out.println("\nPractical examples:");76    77    LocalDate birthday→ 1990-03-15 = LocalDate.of(1990, 3, 15);78    LocalDate today→ 2025-01-29 = LocalDate.of(2025, 1, 29);79    long age→ 34 = ChronoUnit.YEARS.between(birthday1990-03-15, today2025-01-29);80    System.out.println("Age: " + age34 + " years");81    82    LocalDate projectStart→ 2025-01-01 = LocalDate.of(2025, 1, 1);83    LocalDate projectEnd→ 2025-03-31 = LocalDate.of(2025, 3, 31);84    long projectDays→ 89 = ChronoUnit.DAYS.between(projectStart2025-01-01, projectEnd2025-03-31);85    System.out.println("Project duration: " + projectDays89 + " days");86    87    LocalDate subscriptionStart→ 2024-01-01 = LocalDate.of(2024, 1, 1);88    LocalDate subscriptionEnd→ 2025-01-01 = LocalDate.of(2025, 1, 1);89    long subscriptionMonths→ 12 = ChronoUnit.MONTHS.between(subscriptionStart2024-01-01, subscriptionEnd2025-01-01);90    System.out.println("Subscription: " + subscriptionMonths12 + " months");91}
    outputStart: 2025-01-01
    End: 2025-12-31
    Period between:
    Years: 0
    Months: 11
    Days: 30
    Full period: P11M30D
    
    Total days: 364
    
    Other units:
    Weeks: 52
    Months: 11
    Years: 0
    
    Different pairs:
    2020-01-15 to 2025-03-20
    Period: 5y 2m 5d
    Total days: 1891
    
    DateTime differences:
    2025-01-29T10:00 to 2025-01-29T15:30
    Duration: PT5H30M
    Hours: 5
    Minutes: 330
    Seconds: 19800
    
    Time units between:
    Hours: 5
    Minutes: 330
    Seconds: 19800
    
    Absolute difference:
    Past to future: 3653
    Future to past: -3653
    Absolute: 3653
    
    Practical examples:
    Age: 34 years
    Project duration: 89 days
    Subscription: 12 months
  1. start ← 2025-01-01, end ← 2025-02-01, period ← P1M, days ← 31

    11public static void main(String[] args) {12    LocalDate start→ 2025-01-01 = LocalDate.of(2025, 1, 1);13    LocalDate end→ 2025-02-01 = LocalDate.of(2025, 2, 1);14    15    System.out.println("Start: " + start2025-01-01);16    System.out.println("End: " + end2025-02-01);17    System.out.println();1819    // Period between20    Period period→ P1M = Period.between(start2025-01-01, end2025-02-01);21    System.out.println("Period between:");22    System.out.println("Years: " + period.getYears());23    System.out.println("Months: " + period.getMonths());24    System.out.println("Days: " + period.getDays());25    System.out.println("Full period: " + periodP1M);2627    // Total days28    long days→ 31 = ChronoUnit.DAYS.between(start2025-01-01, end2025-02-01);29    System.out.println("\nTotal days: " + days31);3031    // Other units32    System.out.println("\nOther units:");33    System.out.println("Weeks: " + ChronoUnit.WEEKS.between(start2025-01-01, end2025-02-01));34    System.out.println("Months: " + ChronoUnit.MONTHS.between(start2025-01-01, end2025-02-01));35    System.out.println("Years: " + ChronoUnit.YEARS.between(start2025-01-01, end2025-02-01));3637    // Different date pairs38    System.out.println("\nDifferent pairs:");39    LocalDate d1→ 2020-01-15 = LocalDate.of(2020, 1, 15);40    LocalDate d2→ 2025-03-20 = LocalDate.of(2025, 3, 20);41    42    Period p→ P5Y2M5D = Period.between(d12020-01-15, d22025-03-20);43    System.out.println(d12020-01-15 + " to " + d22025-03-20);44    System.out.println("Period: " + p.getYears() + "y " + p.getMonths() + "m " + p.getDays() + "d");45    System.out.println("Total days: " + ChronoUnit.DAYS.between(d12020-01-15, d22025-03-20));4647    // DateTime differences48    System.out.println("\nDateTime differences:");49    LocalDateTime dt1→ 2025-01-29T10:00 = LocalDateTime.of(2025, 1, 29, 10, 0);50    LocalDateTime dt2→ 2025-01-29T15:30 = LocalDateTime.of(2025, 1, 29, 15, 30);51    52    Duration duration→ PT5H30M = Duration.between(dt12025-01-29T10:00, dt22025-01-29T15:30);53    System.out.println(dt12025-01-29T10:00 + " to " + dt22025-01-29T15:30);54    System.out.println("Duration: " + durationPT5H30M);55    System.out.println("Hours: " + duration.toHours());56    System.out.println("Minutes: " + duration.toMinutes());57    System.out.println("Seconds: " + duration.toSeconds());5859    // Time units60    System.out.println("\nTime units between:");61    System.out.println("Hours: " + ChronoUnit.HOURS.between(dt12025-01-29T10:00, dt22025-01-29T15:30));62    System.out.println("Minutes: " + ChronoUnit.MINUTES.between(dt12025-01-29T10:00, dt22025-01-29T15:30));63    System.out.println("Seconds: " + ChronoUnit.SECONDS.between(dt12025-01-29T10:00, dt22025-01-29T15:30));6465    // Absolute difference66    System.out.println("\nAbsolute difference:");67    LocalDate past→ 2020-01-01 = LocalDate.of(2020, 1, 1);68    LocalDate future→ 2030-01-01 = LocalDate.of(2030, 1, 1);69    70    System.out.println("Past to future: " + ChronoUnit.DAYS.between(past2020-01-01, future2030-01-01));71    System.out.println("Future to past: " + ChronoUnit.DAYS.between(future2030-01-01, past2020-01-01));72    System.out.println("Absolute: " + Math.abs(ChronoUnit.DAYS.between(future2030-01-01, past2020-01-01)));7374    // Practical examples75    System.out.println("\nPractical examples:");76    77    LocalDate birthday→ 1990-03-15 = LocalDate.of(1990, 3, 15);78    LocalDate today→ 2025-01-29 = LocalDate.of(2025, 1, 29);79    long age→ 34 = ChronoUnit.YEARS.between(birthday1990-03-15, today2025-01-29);80    System.out.println("Age: " + age34 + " years");81    82    LocalDate projectStart→ 2025-01-01 = LocalDate.of(2025, 1, 1);83    LocalDate projectEnd→ 2025-03-31 = LocalDate.of(2025, 3, 31);84    long projectDays→ 89 = ChronoUnit.DAYS.between(projectStart2025-01-01, projectEnd2025-03-31);85    System.out.println("Project duration: " + projectDays89 + " days");86    87    LocalDate subscriptionStart→ 2024-01-01 = LocalDate.of(2024, 1, 1);88    LocalDate subscriptionEnd→ 2025-01-01 = LocalDate.of(2025, 1, 1);89    long subscriptionMonths→ 12 = ChronoUnit.MONTHS.between(subscriptionStart2024-01-01, subscriptionEnd2025-01-01);90    System.out.println("Subscription: " + subscriptionMonths12 + " months");91}
    outputStart: 2025-01-01
    End: 2025-02-01
    Period between:
    Years: 0
    Months: 1
    Days: 0
    Full period: P1M
    
    Total days: 31
    
    Other units:
    Weeks: 4
    Months: 1
    Years: 0
    
    Different pairs:
    2020-01-15 to 2025-03-20
    Period: 5y 2m 5d
    Total days: 1891
    
    DateTime differences:
    2025-01-29T10:00 to 2025-01-29T15:30
    Duration: PT5H30M
    Hours: 5
    Minutes: 330
    Seconds: 19800
    
    Time units between:
    Hours: 5
    Minutes: 330
    Seconds: 19800
    
    Absolute difference:
    Past to future: 3653
    Future to past: -3653
    Absolute: 3653
    
    Practical examples:
    Age: 34 years
    Project duration: 89 days
    Subscription: 12 months
Duration Represents a time-based amount in seconds and nanoseconds. Used for precise time measurements.

Age Calculation

birthdate
Age.java
Replay: real traced execution (multi-file project)
// Age calculation

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

public class Age {

    public static void main(String[] args) {
        LocalDate birthdate = LocalDate.of(1990, 3, 15);
        LocalDate today = LocalDate.of(2025, 1, 29);

        System.out.println("Birthdate: " + birthdate);
        System.out.println("Today: " + today);
        System.out.println();

        // Age in years
        long ageYears = ChronoUnit.YEARS.between(birthdate, today);
        System.out.println("Age (years): " + ageYears);

        // Age as period
        Period age = Period.between(birthdate, today);
        System.out.println("Age (period): " + age.getYears() + " years, " +
                          age.getMonths() + " months, " + age.getDays() + " days");

        // Age in different units
        System.out.println("\nAge in different units:");
        System.out.println("Years: " + ChronoUnit.YEARS.between(birthdate, today));
        System.out.println("Months: " + ChronoUnit.MONTHS.between(birthdate, today));
        System.out.println("Days: " + ChronoUnit.DAYS.between(birthdate, today));

        // Next birthday
        LocalDate nextBirthday = birthdate.withYear(today.getYear());
        if (nextBirthday.isBefore(today) || nextBirthday.isEqual(today)) {
            nextBirthday = nextBirthday.plusYears(1);
        }
        System.out.println("\nNext birthday: " + nextBirthday);
        System.out.println("Days until birthday: " + ChronoUnit.DAYS.between(today, nextBirthday));

        // Age at specific date
        System.out.println("\nAge at specific dates:");
        LocalDate[] checkDates = {
            LocalDate.of(2020, 1, 1),
            LocalDate.of(2025, 1, 1),
            LocalDate.of(2030, 12, 31)
        };

        for (LocalDate checkDate : checkDates) {
            long ageAt = ChronoUnit.YEARS.between(birthdate, checkDate);
            System.out.println("Age on " + checkDate + ": " + ageAt);
        }

        // Multiple people
        System.out.println("\nMultiple people:");
        LocalDate[][] people = {
            {LocalDate.of(1990, 3, 15), today},
            {LocalDate.of(1985, 7, 22), today},
            {LocalDate.of(2000, 11, 30), today}
        };

        for (int i = 0; i < people.length; i++) {
            LocalDate birth = people[i][0];
            LocalDate current = people[i][1];
            Period p = Period.between(birth, current);
            System.out.println("Person " + (i + 1) + " (born " + birth + "): " +
                             p.getYears() + "y " + p.getMonths() + "m " + p.getDays() + "d");
        }

        // Age verification
        System.out.println("\nAge verification:");
        System.out.println("Is 18 or older? " + isAdult(birthdate, today));
        System.out.println("Is 21 or older? " + isMinimumAge(birthdate, today, 21));
        System.out.println("Is senior (65+)? " + isMinimumAge(birthdate, today, 65));

        // Age groups
        System.out.println("\nAge group:");
        System.out.println(getAgeGroup(birthdate, today));

        // Time until milestone
        System.out.println("\nMilestone birthdays:");
        int[] milestones = {18, 21, 30, 40, 50};
        int currentAge = (int) ChronoUnit.YEARS.between(birthdate, today);

        for (int milestone : milestones) {
            if (milestone > currentAge) {
                LocalDate milestoneDate = birthdate.plusYears(milestone);
                long daysUntil = ChronoUnit.DAYS.between(today, milestoneDate);
                System.out.println(milestone + " years: " + milestoneDate +
                                 " (in " + daysUntil + " days)");
            }
        }

        // Retirement calculation
        System.out.println("\nRetirement:");
        int retirementAge = 65;
        LocalDate retirementDate = birthdate.plusYears(retirementAge);
        long yearsToRetirement = ChronoUnit.YEARS.between(today, retirementDate);
        long daysToRetirement = ChronoUnit.DAYS.between(today, retirementDate);
        System.out.println("Retirement date: " + retirementDate);
        System.out.println("Years to retirement: " + yearsToRetirement);
        System.out.println("Days to retirement: " + daysToRetirement);
    }

    public static boolean isAdult(LocalDate birthdate, LocalDate currentDate) {
        return ChronoUnit.YEARS.between(birthdate, currentDate) >= 18;
    }

    public static boolean isMinimumAge(LocalDate birthdate, LocalDate currentDate, int minAge) {
        return ChronoUnit.YEARS.between(birthdate, currentDate) >= minAge;
    }

    public static String getAgeGroup(LocalDate birthdate, LocalDate currentDate) {
        long age = ChronoUnit.YEARS.between(birthdate, currentDate);
        if (age < 13) return "Child";
        if (age < 18) return "Teenager";
        if (age < 65) return "Adult";
        return "Senior";
    }

}
// Age calculation

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

public class Age {

    public static void main(String[] args) {
        LocalDate birthdate = LocalDate.of(2008, 2, 29);
        LocalDate today = LocalDate.of(2025, 1, 29);

        System.out.println("Birthdate: " + birthdate);
        System.out.println("Today: " + today);
        System.out.println();

        // Age in years
        long ageYears = ChronoUnit.YEARS.between(birthdate, today);
        System.out.println("Age (years): " + ageYears);

        // Age as period
        Period age = Period.between(birthdate, today);
        System.out.println("Age (period): " + age.getYears() + " years, " +
                          age.getMonths() + " months, " + age.getDays() + " days");

        // Age in different units
        System.out.println("\nAge in different units:");
        System.out.println("Years: " + ChronoUnit.YEARS.between(birthdate, today));
        System.out.println("Months: " + ChronoUnit.MONTHS.between(birthdate, today));
        System.out.println("Days: " + ChronoUnit.DAYS.between(birthdate, today));

        // Next birthday
        LocalDate nextBirthday = birthdate.withYear(today.getYear());
        if (nextBirthday.isBefore(today) || nextBirthday.isEqual(today)) {
            nextBirthday = nextBirthday.plusYears(1);
        }
        System.out.println("\nNext birthday: " + nextBirthday);
        System.out.println("Days until birthday: " + ChronoUnit.DAYS.between(today, nextBirthday));

        // Age at specific date
        System.out.println("\nAge at specific dates:");
        LocalDate[] checkDates = {
            LocalDate.of(2020, 1, 1),
            LocalDate.of(2025, 1, 1),
            LocalDate.of(2030, 12, 31)
        };

        for (LocalDate checkDate : checkDates) {
            long ageAt = ChronoUnit.YEARS.between(birthdate, checkDate);
            System.out.println("Age on " + checkDate + ": " + ageAt);
        }

        // Multiple people
        System.out.println("\nMultiple people:");
        LocalDate[][] people = {
            {LocalDate.of(1990, 3, 15), today},
            {LocalDate.of(1985, 7, 22), today},
            {LocalDate.of(2000, 11, 30), today}
        };

        for (int i = 0; i < people.length; i++) {
            LocalDate birth = people[i][0];
            LocalDate current = people[i][1];
            Period p = Period.between(birth, current);
            System.out.println("Person " + (i + 1) + " (born " + birth + "): " +
                             p.getYears() + "y " + p.getMonths() + "m " + p.getDays() + "d");
        }

        // Age verification
        System.out.println("\nAge verification:");
        System.out.println("Is 18 or older? " + isAdult(birthdate, today));
        System.out.println("Is 21 or older? " + isMinimumAge(birthdate, today, 21));
        System.out.println("Is senior (65+)? " + isMinimumAge(birthdate, today, 65));

        // Age groups
        System.out.println("\nAge group:");
        System.out.println(getAgeGroup(birthdate, today));

        // Time until milestone
        System.out.println("\nMilestone birthdays:");
        int[] milestones = {18, 21, 30, 40, 50};
        int currentAge = (int) ChronoUnit.YEARS.between(birthdate, today);

        for (int milestone : milestones) {
            if (milestone > currentAge) {
                LocalDate milestoneDate = birthdate.plusYears(milestone);
                long daysUntil = ChronoUnit.DAYS.between(today, milestoneDate);
                System.out.println(milestone + " years: " + milestoneDate +
                                 " (in " + daysUntil + " days)");
            }
        }

        // Retirement calculation
        System.out.println("\nRetirement:");
        int retirementAge = 65;
        LocalDate retirementDate = birthdate.plusYears(retirementAge);
        long yearsToRetirement = ChronoUnit.YEARS.between(today, retirementDate);
        long daysToRetirement = ChronoUnit.DAYS.between(today, retirementDate);
        System.out.println("Retirement date: " + retirementDate);
        System.out.println("Years to retirement: " + yearsToRetirement);
        System.out.println("Days to retirement: " + daysToRetirement);
    }

    public static boolean isAdult(LocalDate birthdate, LocalDate currentDate) {
        return ChronoUnit.YEARS.between(birthdate, currentDate) >= 18;
    }

    public static boolean isMinimumAge(LocalDate birthdate, LocalDate currentDate, int minAge) {
        return ChronoUnit.YEARS.between(birthdate, currentDate) >= minAge;
    }

    public static String getAgeGroup(LocalDate birthdate, LocalDate currentDate) {
        long age = ChronoUnit.YEARS.between(birthdate, currentDate);
        if (age < 13) return "Child";
        if (age < 18) return "Teenager";
        if (age < 65) return "Adult";
        return "Senior";
    }

}
  1. birthdate ← 1990-03-15, today ← 2025-01-29, ageYears ← 34, age ← P34Y10M14D

    9public static void main(String[] args) {10    LocalDate birthdate→ 1990-03-15 = LocalDate.of(1990, 3, 15); //@birthdate=LocalDate.of(1990, 3, 15), LocalDate.of(2008, 2, 29)11    LocalDate today→ 2025-01-29 = LocalDate.of(2025, 1, 29);12    13    System.out.println("Birthdate: " + birthdate1990-03-15);14    System.out.println("Today: " + today2025-01-29);15    System.out.println();1617    // Age in years18    long ageYears→ 34 = ChronoUnit.YEARS.between(birthdate1990-03-15, today2025-01-29);19    System.out.println("Age (years): " + ageYears34);2021    // Age as period22    Period age→ P34Y10M14D = Period.between(birthdate1990-03-15, today2025-01-29);23    System.out.println("Age (period): " + age.getYears() + " years, " + 24                      age.getMonths() + " months, " + age.getDays() + " days");2526    // Age in different units27    System.out.println("\nAge in different units:");28    System.out.println("Years: " + ChronoUnit.YEARS.between(birthdate1990-03-15, today2025-01-29));29    System.out.println("Months: " + ChronoUnit.MONTHS.between(birthdate1990-03-15, today2025-01-29));30    System.out.println("Days: " + ChronoUnit.DAYS.between(birthdate1990-03-15, today2025-01-29));3132    // Next birthday33    LocalDate nextBirthday→ 2025-03-15 = birthdate.withYear(today.getYear());34    if (nextBirthday.isBefore(today) || nextBirthday.isEqual(today)) {35        nextBirthday = nextBirthday.plusYears(1);36    }37    System.out.println("\nNext birthday: " + nextBirthday2025-03-15);38    System.out.println("Days until birthday: " + ChronoUnit.DAYS.between(today2025-01-29, nextBirthday2025-03-15));3940    // Age at specific date41    System.out.println("\nAge at specific dates:");42    LocalDate[] checkDates = {43        LocalDate.of(2020, 1, 1),44        LocalDate.of(2025, 1, 1),45        LocalDate.of(2030, 12, 31)46    };
    outputBirthdate: 1990-03-15
    Today: 2025-01-29
    Age (years): 34
    Age (period): 34 years, 10 months, 14 days
    
    Age in different units:
    Years: 34
    Months: 418
    Days: 12739
    
    Next birthday: 2025-03-15
    Days until birthday: 45
    
    Age at specific dates:
  2. ageAt ← 29

    pass 1 of 3
    48for (LocalDate checkDate2020-01-01 : checkDates) {49    long ageAt→ 29 = ChronoUnit.YEARS.between(birthdate1990-03-15, checkDate2020-01-01);50    System.out.println("Age on " + checkDate2020-01-01 + ": " + ageAt29);51}
    outputAge on 2020-01-01: 29
    All 3 passes — pass 1 is the card above
    passcheckDateageAt
    12020-01-0129
    22025-01-0134
    32030-12-3140
  3. LocalDate[][] people =

    53// Multiple people54System.out.println("\nMultiple people:");55LocalDate[][] people = {56    {LocalDate.of(1990, 3, 15), today},57    {LocalDate.of(1985, 7, 22), today},58    {LocalDate.of(2000, 11, 30), today}59};
    output
    Multiple people:
  4. birth ← 1990-03-15, current ← 2025-01-29, p ← P34Y10M14D

    pass 1 of 3
    61for (int i0 = 0; i < people.length3; i++) {62    LocalDate birth→ 1990-03-15 = people[i][0]1990-03-15;63    LocalDate current→ 2025-01-29 = people[i][1]2025-01-29;64    Period p→ P34Y10M14D = Period.between(birth1990-03-15, current2025-01-29);65    System.out.println("Person " + (i0 + 1) + " (born " + birth1990-03-15 + "): " + 66                     p.getYears() + "y " + p.getMonths() + "m " + p.getDays() + "d");67}
    outputPerson 1 (born 1990-03-15): 34y 10m 14d
    All 3 passes — pass 1 is the card above
    passipeople[i][0]birthcurrentp
    101990-03-151990-03-152025-01-29P34Y10M14D
    211985-07-221985-07-222025-01-29P39Y6M7D
    322000-11-302000-11-302025-01-29P24Y1M30D
  5. System.out.println("Is 18 or older? " + isAdult(birthdate, today));

    69// Age verification70System.out.println("\nAge verification:");71System.out.println("Is 18 or older? " + isAdult(birthdate1990-03-15, today2025-01-29));72System.out.println("Is 21 or older? " + isMinimumAge(birthdate, today, 21));
    output
    Age verification:
  6. public static boolean isAdult(LocalDate birthdate, LocalDate currentDa…

    104public static boolean isAdult(LocalDate birthdate1990-03-15, LocalDate currentDate2025-01-29) {105    return ChronoUnit.YEARS.between(birthdate1990-03-15, currentDate2025-01-29) >= 18;106}
  7. System.out.println("Is 18 or older? " + isAdult(birthdate, today));

    70System.out.println("\nAge verification:");71System.out.println("Is 18 or older? " + isAdult(birthdate1990-03-15, today2025-01-29));72System.out.println("Is 21 or older? " + isMinimumAge(birthdate1990-03-15, today2025-01-29, 21));73System.out.println("Is senior (65+)? " + isMinimumAge(birthdate, today, 65));
    outputIs 18 or older? true
  8. public static boolean isMinimumAge(LocalDate birthdate, LocalDate curr…

    pass 1 of 2
    108public static boolean isMinimumAge(LocalDate birthdate1990-03-15, LocalDate currentDate2025-01-29, int minAge21) {109    return ChronoUnit.YEARS.between(birthdate1990-03-15, currentDate2025-01-29) >= minAge21;110}
  9. System.out.println("Is 21 or older? " + isMinimumAge(birthdate, today,…

    71System.out.println("Is 18 or older? " + isAdult(birthdate, today));72System.out.println("Is 21 or older? " + isMinimumAge(birthdate1990-03-15, today2025-01-29, 21));73System.out.println("Is senior (65+)? " + isMinimumAge(birthdate1990-03-15, today2025-01-29, 65));
    outputIs 21 or older? true
  10. public static boolean isMinimumAge(LocalDate birthdate, LocalDate curr…

    pass 2 of 2
    108public static boolean isMinimumAge(LocalDate birthdate1990-03-15, LocalDate currentDate2025-01-29, int minAge65) {109    return ChronoUnit.YEARS.between(birthdate1990-03-15, currentDate2025-01-29) >= minAge65;110}
  11. System.out.println("Is senior (65+)? " + isMinimumAge(birthdate, today…

    72System.out.println("Is 21 or older? " + isMinimumAge(birthdate, today, 21));73System.out.println("Is senior (65+)? " + isMinimumAge(birthdate1990-03-15, today2025-01-29, 65));7475// Age groups76System.out.println("\nAge group:");77System.out.println(getAgeGroup(birthdate1990-03-15, today2025-01-29));
    outputIs senior (65+)? false
    
    Age group:
  12. age ← 34

    112public static String getAgeGroup(LocalDate birthdate1990-03-15, LocalDate currentDate2025-01-29) {113    long age→ 34 = ChronoUnit.YEARS.between(birthdate1990-03-15, currentDate2025-01-29);114    if (age < 13) return "Child";
  13. if (age < 65)

    115if (age < 18) return "Teenager";116if (age34 < 65) return "Adult";117return "Senior";
  14. currentAge ← 34

    76System.out.println("\nAge group:");77System.out.println(getAgeGroup(birthdate1990-03-15, today2025-01-29));7879// Time until milestone80System.out.println("\nMilestone birthdays:");81int[] milestones = {18, 21, 30, 40, 50};82int currentAge→ 34 = (int) ChronoUnit.YEARS.between(birthdate1990-03-15, today2025-01-29);
    outputAdult
    
    Milestone birthdays:
  15. for (int milestone : milestones)

    pass 1 of 5
    84for (int milestone18 : milestones) {85    if (milestone > currentAge) {
    All 5 passes — pass 1 is the card above
    passmilestonecurrentAgetodaymilestoneDatedaysUntil
    118
    221
    330
    440342025-01-292030-03-151871
    550342025-01-292040-03-155524
  16. milestoneDate ← 2030-03-15, daysUntil ← 1871

    pass 1 of 2
    84for (int milestone : milestones) {85    if (milestone40 > currentAge34) {86        LocalDate milestoneDate→ 2030-03-15 = birthdate.plusYears(milestone40);87        long daysUntil→ 1871 = ChronoUnit.DAYS.between(today2025-01-29, milestoneDate2030-03-15);88        System.out.println(milestone40 + " years: " + milestoneDate2030-03-15 + 89                         " (in " + daysUntil1871 + " days)");90    }
    output40 years: 2030-03-15 (in 1871 days)
  17. milestoneDate ← 2040-03-15, daysUntil ← 5524

    pass 2 of 2
    84for (int milestone : milestones) {85    if (milestone50 > currentAge34) {86        LocalDate milestoneDate→ 2040-03-15 = birthdate.plusYears(milestone50);87        long daysUntil→ 5524 = ChronoUnit.DAYS.between(today2025-01-29, milestoneDate2040-03-15);88        System.out.println(milestone50 + " years: " + milestoneDate2040-03-15 + 89                         " (in " + daysUntil5524 + " days)");90    }
    output50 years: 2040-03-15 (in 5524 days)
  18. retirementAge ← 65, retirementDate ← 2055-03-15, yearsToRetirement ← 30

    93    // Retirement calculation94    System.out.println("\nRetirement:");95    int retirementAge→ 65 = 65;96    LocalDate retirementDate→ 2055-03-15 = birthdate.plusYears(retirementAge65);97    long yearsToRetirement→ 30 = ChronoUnit.YEARS.between(today2025-01-29, retirementDate2055-03-15);98    long daysToRetirement→ 11002 = ChronoUnit.DAYS.between(today2025-01-29, retirementDate2055-03-15);99    System.out.println("Retirement date: " + retirementDate2055-03-15);100    System.out.println("Years to retirement: " + yearsToRetirement30);101    System.out.println("Days to retirement: " + daysToRetirement11002);102}
    output
    Retirement:
    Retirement date: 2055-03-15
    Years to retirement: 30
    Days to retirement: 11002
  1. birthdate ← 2008-02-29, today ← 2025-01-29, ageYears ← 16, age ← P16Y11M

    9public static void main(String[] args) {10    LocalDate birthdate→ 2008-02-29 = LocalDate.of(2008, 2, 29);11    LocalDate today→ 2025-01-29 = LocalDate.of(2025, 1, 29);12    13    System.out.println("Birthdate: " + birthdate2008-02-29);14    System.out.println("Today: " + today2025-01-29);15    System.out.println();1617    // Age in years18    long ageYears→ 16 = ChronoUnit.YEARS.between(birthdate2008-02-29, today2025-01-29);19    System.out.println("Age (years): " + ageYears16);2021    // Age as period22    Period age→ P16Y11M = Period.between(birthdate2008-02-29, today2025-01-29);23    System.out.println("Age (period): " + age.getYears() + " years, " + 24                      age.getMonths() + " months, " + age.getDays() + " days");2526    // Age in different units27    System.out.println("\nAge in different units:");28    System.out.println("Years: " + ChronoUnit.YEARS.between(birthdate2008-02-29, today2025-01-29));29    System.out.println("Months: " + ChronoUnit.MONTHS.between(birthdate2008-02-29, today2025-01-29));30    System.out.println("Days: " + ChronoUnit.DAYS.between(birthdate2008-02-29, today2025-01-29));3132    // Next birthday33    LocalDate nextBirthday→ 2025-02-28 = birthdate.withYear(today.getYear());34    if (nextBirthday.isBefore(today) || nextBirthday.isEqual(today)) {35        nextBirthday = nextBirthday.plusYears(1);36    }37    System.out.println("\nNext birthday: " + nextBirthday2025-02-28);38    System.out.println("Days until birthday: " + ChronoUnit.DAYS.between(today2025-01-29, nextBirthday2025-02-28));3940    // Age at specific date41    System.out.println("\nAge at specific dates:");42    LocalDate[] checkDates = {43        LocalDate.of(2020, 1, 1),44        LocalDate.of(2025, 1, 1),45        LocalDate.of(2030, 12, 31)46    };
    outputBirthdate: 2008-02-29
    Today: 2025-01-29
    Age (years): 16
    Age (period): 16 years, 11 months, 0 days
    
    Age in different units:
    Years: 16
    Months: 203
    Days: 6179
    
    Next birthday: 2025-02-28
    Days until birthday: 30
    
    Age at specific dates:
  2. ageAt ← 11

    pass 1 of 3
    48for (LocalDate checkDate2020-01-01 : checkDates) {49    long ageAt→ 11 = ChronoUnit.YEARS.between(birthdate2008-02-29, checkDate2020-01-01);50    System.out.println("Age on " + checkDate2020-01-01 + ": " + ageAt11);51}
    outputAge on 2020-01-01: 11
    All 3 passes — pass 1 is the card above
    passcheckDateageAt
    12020-01-0111
    22025-01-0116
    32030-12-3122
  3. LocalDate[][] people =

    53// Multiple people54System.out.println("\nMultiple people:");55LocalDate[][] people = {56    {LocalDate.of(1990, 3, 15), today},57    {LocalDate.of(1985, 7, 22), today},58    {LocalDate.of(2000, 11, 30), today}59};
    output
    Multiple people:
  4. birth ← 1990-03-15, current ← 2025-01-29, p ← P34Y10M14D

    pass 1 of 3
    61for (int i0 = 0; i < people.length3; i++) {62    LocalDate birth→ 1990-03-15 = people[i][0]1990-03-15;63    LocalDate current→ 2025-01-29 = people[i][1]2025-01-29;64    Period p→ P34Y10M14D = Period.between(birth1990-03-15, current2025-01-29);65    System.out.println("Person " + (i0 + 1) + " (born " + birth1990-03-15 + "): " + 66                     p.getYears() + "y " + p.getMonths() + "m " + p.getDays() + "d");67}
    outputPerson 1 (born 1990-03-15): 34y 10m 14d
    All 3 passes — pass 1 is the card above
    passipeople[i][0]birthcurrentp
    101990-03-151990-03-152025-01-29P34Y10M14D
    211985-07-221985-07-222025-01-29P39Y6M7D
    322000-11-302000-11-302025-01-29P24Y1M30D
  5. System.out.println("Is 18 or older? " + isAdult(birthdate, today));

    69// Age verification70System.out.println("\nAge verification:");71System.out.println("Is 18 or older? " + isAdult(birthdate2008-02-29, today2025-01-29));72System.out.println("Is 21 or older? " + isMinimumAge(birthdate, today, 21));
    output
    Age verification:
  6. public static boolean isAdult(LocalDate birthdate, LocalDate currentDa…

    104public static boolean isAdult(LocalDate birthdate2008-02-29, LocalDate currentDate2025-01-29) {105    return ChronoUnit.YEARS.between(birthdate2008-02-29, currentDate2025-01-29) >= 18;106}
  7. System.out.println("Is 18 or older? " + isAdult(birthdate, today));

    70System.out.println("\nAge verification:");71System.out.println("Is 18 or older? " + isAdult(birthdate2008-02-29, today2025-01-29));72System.out.println("Is 21 or older? " + isMinimumAge(birthdate2008-02-29, today2025-01-29, 21));73System.out.println("Is senior (65+)? " + isMinimumAge(birthdate, today, 65));
    outputIs 18 or older? false
  8. public static boolean isMinimumAge(LocalDate birthdate, LocalDate curr…

    pass 1 of 2
    108public static boolean isMinimumAge(LocalDate birthdate2008-02-29, LocalDate currentDate2025-01-29, int minAge21) {109    return ChronoUnit.YEARS.between(birthdate2008-02-29, currentDate2025-01-29) >= minAge21;110}
  9. System.out.println("Is 21 or older? " + isMinimumAge(birthdate, today,…

    71System.out.println("Is 18 or older? " + isAdult(birthdate, today));72System.out.println("Is 21 or older? " + isMinimumAge(birthdate2008-02-29, today2025-01-29, 21));73System.out.println("Is senior (65+)? " + isMinimumAge(birthdate2008-02-29, today2025-01-29, 65));
    outputIs 21 or older? false
  10. public static boolean isMinimumAge(LocalDate birthdate, LocalDate curr…

    pass 2 of 2
    108public static boolean isMinimumAge(LocalDate birthdate2008-02-29, LocalDate currentDate2025-01-29, int minAge65) {109    return ChronoUnit.YEARS.between(birthdate2008-02-29, currentDate2025-01-29) >= minAge65;110}
  11. System.out.println("Is senior (65+)? " + isMinimumAge(birthdate, today…

    72System.out.println("Is 21 or older? " + isMinimumAge(birthdate, today, 21));73System.out.println("Is senior (65+)? " + isMinimumAge(birthdate2008-02-29, today2025-01-29, 65));7475// Age groups76System.out.println("\nAge group:");77System.out.println(getAgeGroup(birthdate2008-02-29, today2025-01-29));
    outputIs senior (65+)? false
    
    Age group:
  12. age ← 16

    112public static String getAgeGroup(LocalDate birthdate2008-02-29, LocalDate currentDate2025-01-29) {113    long age→ 16 = ChronoUnit.YEARS.between(birthdate2008-02-29, currentDate2025-01-29);114    if (age < 13) return "Child";
  13. if (age < 18)

    114if (age < 13) return "Child";115if (age16 < 18) return "Teenager";116if (age < 65) return "Adult";
  14. currentAge ← 16

    76System.out.println("\nAge group:");77System.out.println(getAgeGroup(birthdate2008-02-29, today2025-01-29));7879// Time until milestone80System.out.println("\nMilestone birthdays:");81int[] milestones = {18, 21, 30, 40, 50};82int currentAge→ 16 = (int) ChronoUnit.YEARS.between(birthdate2008-02-29, today2025-01-29);
    outputTeenager
    
    Milestone birthdays:
  15. for (int milestone : milestones)

    pass 1 of 5
    84for (int milestone18 : milestones) {85    if (milestone > currentAge) {
    All 5 passes — pass 1 is the card above
    passmilestone
    118
    221
    330
    440
    550
  16. milestoneDate ← 2026-02-28, daysUntil ← 395

    pass 1 of 5
    84for (int milestone : milestones) {85    if (milestone18 > currentAge16) {86        LocalDate milestoneDate→ 2026-02-28 = birthdate.plusYears(milestone18);87        long daysUntil→ 395 = ChronoUnit.DAYS.between(today2025-01-29, milestoneDate2026-02-28);88        System.out.println(milestone18 + " years: " + milestoneDate2026-02-28 + 89                         " (in " + daysUntil395 + " days)");90    }
    output18 years: 2026-02-28 (in 395 days)
    All 5 passes — pass 1 is the card above
    passmilestonemilestoneDatedaysUntil
    1182026-02-28395
    2212029-02-281491
    3302038-02-284778
    4402048-02-298431
    5502058-02-2812083
  17. retirementAge ← 65, retirementDate ← 2073-02-28, yearsToRetirement ← 48

    93    // Retirement calculation94    System.out.println("\nRetirement:");95    int retirementAge→ 65 = 65;96    LocalDate retirementDate→ 2073-02-28 = birthdate.plusYears(retirementAge65);97    long yearsToRetirement→ 48 = ChronoUnit.YEARS.between(today2025-01-29, retirementDate2073-02-28);98    long daysToRetirement→ 17562 = ChronoUnit.DAYS.between(today2025-01-29, retirementDate2073-02-28);99    System.out.println("Retirement date: " + retirementDate2073-02-28);100    System.out.println("Years to retirement: " + yearsToRetirement48);101    System.out.println("Days to retirement: " + daysToRetirement17562);102}
    output
    Retirement:
    Retirement date: 2073-02-28
    Years to retirement: 48
    Days to retirement: 17562

Day of Week

example
Dayofweek.java
Replay: real traced execution (multi-file project)
// Day of week operations

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

public class Dayofweek {

    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2025, 1, 29);
        System.out.println("Date: " + date);
        System.out.println();

        // Get day of week
        DayOfWeek dow = date.getDayOfWeek();
        System.out.println("Day of week: " + dow);
        System.out.println("Day value (1=Mon, 7=Sun): " + dow.getValue());

        // Check specific days
        System.out.println("\nCheck specific days:");
        System.out.println("Is Monday? " + (dow == DayOfWeek.MONDAY));
        System.out.println("Is weekend? " + (dow == DayOfWeek.SATURDAY || dow == DayOfWeek.SUNDAY));

        // Next/previous specific day
        System.out.println("\nNext/previous specific day:");
        LocalDate nextMonday = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        System.out.println("Next Monday: " + nextMonday);

        LocalDate prevFriday = date.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));
        System.out.println("Previous Friday: " + prevFriday);

        // Next or same
        LocalDate nextOrSameMon = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
        System.out.println("Next or same Monday: " + nextOrSameMon);

        // Days in week
        System.out.println("\nAll days of week:");
        for (DayOfWeek day : DayOfWeek.values()) {
            System.out.println(day.getValue() + ": " + day);
        }

        // Week navigation
        System.out.println("\nWeek navigation:");
        LocalDate monday = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        System.out.println("This week:");
        for (int i = 0; i < 7; i++) {
            LocalDate d = monday.plusDays(i);
            System.out.println(d.getDayOfWeek() + ": " + d);
        }

        // Business days
        System.out.println("\nBusiness days:");
        System.out.println("Next business day: " + nextBusinessDay(date));
        System.out.println("Previous business day: " + previousBusinessDay(date));

        // Count business days
        LocalDate start = LocalDate.of(2025, 1, 27);  // Monday
        LocalDate end = LocalDate.of(2025, 2, 7);    // Friday
        System.out.println("\nBusiness days from " + start + " to " + end + ": " +
                          countBusinessDays(start, end));

        // Weekend check
        System.out.println("\nWeekend check:");
        LocalDate[] testDates = {
            LocalDate.of(2025, 1, 27),  // Monday
            LocalDate.of(2025, 2, 1),   // Saturday
            LocalDate.of(2025, 2, 2)    // Sunday
        };

        for (LocalDate d : testDates) {
            System.out.println(d + " (" + d.getDayOfWeek() + "): " +
                             (isWeekend(d) ? "Weekend" : "Weekday"));
        }

        // Add business days
        System.out.println("\nAdd business days:");
        LocalDate today = LocalDate.of(2025, 1, 29);  // Wednesday
        int businessDaysToAdd = 5;
        System.out.println("Today: " + today + " (" + today.getDayOfWeek() + ")");
        System.out.println("+" + businessDaysToAdd + " business days: " + addBusinessDays(today, businessDaysToAdd));
    }

    public static LocalDate nextBusinessDay(LocalDate date) {
        LocalDate next = date.plusDays(1);
        while (isWeekend(next)) {
            next = next.plusDays(1);
        }
        return next;
    }

    public static LocalDate previousBusinessDay(LocalDate date) {
        LocalDate prev = date.minusDays(1);
        while (isWeekend(prev)) {
            prev = prev.minusDays(1);
        }
        return prev;
    }

    public static boolean isWeekend(LocalDate date) {
        DayOfWeek dow = date.getDayOfWeek();
        return dow == DayOfWeek.SATURDAY || dow == DayOfWeek.SUNDAY;
    }

    public static long countBusinessDays(LocalDate start, LocalDate end) {
        long count = 0;
        LocalDate current = start;
        while (!current.isAfter(end)) {
            if (!isWeekend(current)) {
                count++;
            }
            current = current.plusDays(1);
        }
        return count;
    }

    public static LocalDate addBusinessDays(LocalDate date, int days) {
        LocalDate result = date;
        int added = 0;
        while (added < days) {
            result = result.plusDays(1);
            if (!isWeekend(result)) {
                added++;
            }
        }
        return result;
    }

}
// Day of week operations

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

public class Dayofweek {

    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2025, 2, 1);
        System.out.println("Date: " + date);
        System.out.println();

        // Get day of week
        DayOfWeek dow = date.getDayOfWeek();
        System.out.println("Day of week: " + dow);
        System.out.println("Day value (1=Mon, 7=Sun): " + dow.getValue());

        // Check specific days
        System.out.println("\nCheck specific days:");
        System.out.println("Is Monday? " + (dow == DayOfWeek.MONDAY));
        System.out.println("Is weekend? " + (dow == DayOfWeek.SATURDAY || dow == DayOfWeek.SUNDAY));

        // Next/previous specific day
        System.out.println("\nNext/previous specific day:");
        LocalDate nextMonday = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        System.out.println("Next Monday: " + nextMonday);

        LocalDate prevFriday = date.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));
        System.out.println("Previous Friday: " + prevFriday);

        // Next or same
        LocalDate nextOrSameMon = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
        System.out.println("Next or same Monday: " + nextOrSameMon);

        // Days in week
        System.out.println("\nAll days of week:");
        for (DayOfWeek day : DayOfWeek.values()) {
            System.out.println(day.getValue() + ": " + day);
        }

        // Week navigation
        System.out.println("\nWeek navigation:");
        LocalDate monday = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        System.out.println("This week:");
        for (int i = 0; i < 7; i++) {
            LocalDate d = monday.plusDays(i);
            System.out.println(d.getDayOfWeek() + ": " + d);
        }

        // Business days
        System.out.println("\nBusiness days:");
        System.out.println("Next business day: " + nextBusinessDay(date));
        System.out.println("Previous business day: " + previousBusinessDay(date));

        // Count business days
        LocalDate start = LocalDate.of(2025, 1, 27);  // Monday
        LocalDate end = LocalDate.of(2025, 2, 7);    // Friday
        System.out.println("\nBusiness days from " + start + " to " + end + ": " +
                          countBusinessDays(start, end));

        // Weekend check
        System.out.println("\nWeekend check:");
        LocalDate[] testDates = {
            LocalDate.of(2025, 1, 27),  // Monday
            LocalDate.of(2025, 2, 1),   // Saturday
            LocalDate.of(2025, 2, 2)    // Sunday
        };

        for (LocalDate d : testDates) {
            System.out.println(d + " (" + d.getDayOfWeek() + "): " +
                             (isWeekend(d) ? "Weekend" : "Weekday"));
        }

        // Add business days
        System.out.println("\nAdd business days:");
        LocalDate today = LocalDate.of(2025, 1, 29);  // Wednesday
        int businessDaysToAdd = 5;
        System.out.println("Today: " + today + " (" + today.getDayOfWeek() + ")");
        System.out.println("+" + businessDaysToAdd + " business days: " + addBusinessDays(today, businessDaysToAdd));
    }

    public static LocalDate nextBusinessDay(LocalDate date) {
        LocalDate next = date.plusDays(1);
        while (isWeekend(next)) {
            next = next.plusDays(1);
        }
        return next;
    }

    public static LocalDate previousBusinessDay(LocalDate date) {
        LocalDate prev = date.minusDays(1);
        while (isWeekend(prev)) {
            prev = prev.minusDays(1);
        }
        return prev;
    }

    public static boolean isWeekend(LocalDate date) {
        DayOfWeek dow = date.getDayOfWeek();
        return dow == DayOfWeek.SATURDAY || dow == DayOfWeek.SUNDAY;
    }

    public static long countBusinessDays(LocalDate start, LocalDate end) {
        long count = 0;
        LocalDate current = start;
        while (!current.isAfter(end)) {
            if (!isWeekend(current)) {
                count++;
            }
            current = current.plusDays(1);
        }
        return count;
    }

    public static LocalDate addBusinessDays(LocalDate date, int days) {
        LocalDate result = date;
        int added = 0;
        while (added < days) {
            result = result.plusDays(1);
            if (!isWeekend(result)) {
                added++;
            }
        }
        return result;
    }

}
// Day of week operations

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

public class Dayofweek {

    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2025, 1, 29);
        System.out.println("Date: " + date);
        System.out.println();

        // Get day of week
        DayOfWeek dow = date.getDayOfWeek();
        System.out.println("Day of week: " + dow);
        System.out.println("Day value (1=Mon, 7=Sun): " + dow.getValue());

        // Check specific days
        System.out.println("\nCheck specific days:");
        System.out.println("Is Monday? " + (dow == DayOfWeek.MONDAY));
        System.out.println("Is weekend? " + (dow == DayOfWeek.SATURDAY || dow == DayOfWeek.SUNDAY));

        // Next/previous specific day
        System.out.println("\nNext/previous specific day:");
        LocalDate nextMonday = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        System.out.println("Next Monday: " + nextMonday);

        LocalDate prevFriday = date.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));
        System.out.println("Previous Friday: " + prevFriday);

        // Next or same
        LocalDate nextOrSameMon = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
        System.out.println("Next or same Monday: " + nextOrSameMon);

        // Days in week
        System.out.println("\nAll days of week:");
        for (DayOfWeek day : DayOfWeek.values()) {
            System.out.println(day.getValue() + ": " + day);
        }

        // Week navigation
        System.out.println("\nWeek navigation:");
        LocalDate monday = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        System.out.println("This week:");
        for (int i = 0; i < 7; i++) {
            LocalDate d = monday.plusDays(i);
            System.out.println(d.getDayOfWeek() + ": " + d);
        }

        // Business days
        System.out.println("\nBusiness days:");
        System.out.println("Next business day: " + nextBusinessDay(date));
        System.out.println("Previous business day: " + previousBusinessDay(date));

        // Count business days
        LocalDate start = LocalDate.of(2025, 1, 27);  // Monday
        LocalDate end = LocalDate.of(2025, 2, 7);    // Friday
        System.out.println("\nBusiness days from " + start + " to " + end + ": " +
                          countBusinessDays(start, end));

        // Weekend check
        System.out.println("\nWeekend check:");
        LocalDate[] testDates = {
            LocalDate.of(2025, 1, 27),  // Monday
            LocalDate.of(2025, 2, 1),   // Saturday
            LocalDate.of(2025, 2, 2)    // Sunday
        };

        for (LocalDate d : testDates) {
            System.out.println(d + " (" + d.getDayOfWeek() + "): " +
                             (isWeekend(d) ? "Weekend" : "Weekday"));
        }

        // Add business days
        System.out.println("\nAdd business days:");
        LocalDate today = LocalDate.of(2025, 1, 29);  // Wednesday
        int businessDaysToAdd = 2;
        System.out.println("Today: " + today + " (" + today.getDayOfWeek() + ")");
        System.out.println("+" + businessDaysToAdd + " business days: " + addBusinessDays(today, businessDaysToAdd));
    }

    public static LocalDate nextBusinessDay(LocalDate date) {
        LocalDate next = date.plusDays(1);
        while (isWeekend(next)) {
            next = next.plusDays(1);
        }
        return next;
    }

    public static LocalDate previousBusinessDay(LocalDate date) {
        LocalDate prev = date.minusDays(1);
        while (isWeekend(prev)) {
            prev = prev.minusDays(1);
        }
        return prev;
    }

    public static boolean isWeekend(LocalDate date) {
        DayOfWeek dow = date.getDayOfWeek();
        return dow == DayOfWeek.SATURDAY || dow == DayOfWeek.SUNDAY;
    }

    public static long countBusinessDays(LocalDate start, LocalDate end) {
        long count = 0;
        LocalDate current = start;
        while (!current.isAfter(end)) {
            if (!isWeekend(current)) {
                count++;
            }
            current = current.plusDays(1);
        }
        return count;
    }

    public static LocalDate addBusinessDays(LocalDate date, int days) {
        LocalDate result = date;
        int added = 0;
        while (added < days) {
            result = result.plusDays(1);
            if (!isWeekend(result)) {
                added++;
            }
        }
        return result;
    }

}
  1. date ← 2025-01-29, dow ← WEDNESDAY, nextMonday ← 2025-02-03, prevFriday ← 2025-01-24

    10public static void main(String[] args) {11    LocalDate date→ 2025-01-29 = LocalDate.of(2025, 1, 29); //@date=LocalDate.of(2025, 1, 29), LocalDate.of(2025, 2, 1)12    System.out.println("Date: " + date2025-01-29);13    System.out.println();1415    // Get day of week16    DayOfWeek dow→ WEDNESDAY = date.getDayOfWeek();17    System.out.println("Day of week: " + dowWEDNESDAY);18    System.out.println("Day value (1=Mon, 7=Sun): " + dow.getValue());1920    // Check specific days21    System.out.println("\nCheck specific days:");22    System.out.println("Is Monday? " + (dowWEDNESDAY == DayOfWeek.MONDAY));23    System.out.println("Is weekend? " + (dowWEDNESDAY == DayOfWeek.SATURDAY || dow == DayOfWeek.SUNDAY));2425    // Next/previous specific day26    System.out.println("\nNext/previous specific day:");27    LocalDate nextMonday→ 2025-02-03 = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));28    System.out.println("Next Monday: " + nextMonday2025-02-03);29    30    LocalDate prevFriday→ 2025-01-24 = date.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));31    System.out.println("Previous Friday: " + prevFriday2025-01-24);3233    // Next or same34    LocalDate nextOrSameMon→ 2025-02-03 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));35    System.out.println("Next or same Monday: " + nextOrSameMon2025-02-03);3637    // Days in week38    System.out.println("\nAll days of week:");39    for (DayOfWeek day : DayOfWeek.values()) {
    outputDate: 2025-01-29
    Day of week: WEDNESDAY
    Day value (1=Mon, 7=Sun): 3
    
    Check specific days:
    Is Monday? false
    Is weekend? false
    
    Next/previous specific day:
    Next Monday: 2025-02-03
    Previous Friday: 2025-01-24
    Next or same Monday: 2025-02-03
    
    All days of week:
  2. for (DayOfWeek day : DayOfWeek.values())

    pass 1 of 7
    38System.out.println("\nAll days of week:");39for (DayOfWeek dayMONDAY : DayOfWeek.values()) {40    System.out.println(day.getValue() + ": " + dayMONDAY);41}
    output1: MONDAY
    All 7 passes — pass 1 is the card above
    passday
    1MONDAY
    2TUESDAY
    3WEDNESDAY
    4THURSDAY
    5FRIDAY
    6SATURDAY
    7SUNDAY
  3. monday ← 2025-01-27

    43// Week navigation44System.out.println("\nWeek navigation:");45LocalDate monday→ 2025-01-27 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));46System.out.println("This week:");47for (int i = 0; i < 7; i++) {
    output
    Week navigation:
    This week:
  4. d ← 2025-01-27

    pass 1 of 7
    46System.out.println("This week:");47for (int i0 = 0; i < 7; i++) {48    LocalDate d→ 2025-01-27 = monday.plusDays(i0);49    System.out.println(d.getDayOfWeek() + ": " + d2025-01-27);50}
    outputMONDAY: 2025-01-27
    All 7 passes — pass 1 is the card above
    passid
    102025-01-27
    212025-01-28
    322025-01-29
    432025-01-30
    542025-01-31
    652025-02-01
    762025-02-02
  5. System.out.println("Next business day: " + nextBusinessDay(date));

    52// Business days53System.out.println("\nBusiness days:");54System.out.println("Next business day: " + nextBusinessDay(date2025-01-29));55System.out.println("Previous business day: " + previousBusinessDay(date));
    output
    Business days:
  6. next ← 2025-01-30

    84public static LocalDate nextBusinessDay(LocalDate date2025-01-29) {85    LocalDate next→ 2025-01-30 = date.plusDays(1);86    while (isWeekend(next)) {
  7. dow ← THURSDAY

    pass 1 of 24
    100public static boolean isWeekend(LocalDate date2025-01-30) {101    DayOfWeek dow→ THURSDAY = date.getDayOfWeek();102    return dowTHURSDAY == DayOfWeek.SATURDAY || dow == DayOfWeek.SUNDAY;103}
    24 passes — pass 1 is the card above
    passdatedow
    12025-01-30THURSDAY
    22025-01-28TUESDAY
    32025-01-27MONDAY
    42025-01-28TUESDAY
    52025-01-29WEDNESDAY
    62025-01-30THURSDAY
    72025-01-31FRIDAY
    82025-02-01SATURDAY
    92025-02-02SUNDAY
    ⋯ 13 more passes ⋯
    232025-02-04TUESDAY
    242025-02-05WEDNESDAY
  8. return next;

    88    }89    return next2025-01-30;90}
  9. System.out.println("Next business day: " + nextBusinessDay(date));

    53System.out.println("\nBusiness days:");54System.out.println("Next business day: " + nextBusinessDay(date2025-01-29));55System.out.println("Previous business day: " + previousBusinessDay(date2025-01-29));
    outputNext business day: 2025-01-30
  10. prev ← 2025-01-28

    92public static LocalDate previousBusinessDay(LocalDate date2025-01-29) {93    LocalDate prev→ 2025-01-28 = date.minusDays(1);94    while (isWeekend(prev)) {
  11. return prev;

    96    }97    return prev2025-01-28;98}
  12. start ← 2025-01-27, end ← 2025-02-07

    54System.out.println("Next business day: " + nextBusinessDay(date));55System.out.println("Previous business day: " + previousBusinessDay(date2025-01-29));5657// Count business days58LocalDate start→ 2025-01-27 = LocalDate.of(2025, 1, 27);  // Monday59LocalDate end→ 2025-02-07 = LocalDate.of(2025, 2, 7);    // Friday60System.out.println("\nBusiness days from " + start2025-01-27 + " to " + end2025-02-07 + ": " + 61                  countBusinessDays(start2025-01-27, end2025-02-07));
    outputPrevious business day: 2025-01-28
  13. count ← 0, current ← 2025-01-27

    105public static long countBusinessDays(LocalDate start2025-01-27, LocalDate end2025-02-07) {106    long count→ 0 = 0;107    LocalDate current→ 2025-01-27 = start;108    while (!current.isAfter(end)) {
  14. while (!current.isAfter(end))

    pass 1 of 12
    107LocalDate current = start;108while (!current.isAfter(end2025-02-07)) {109    if (!isWeekend(current)) {
  15. count ← 1

    pass 1 of 10
    108while (!current.isAfter(end)) {109    if (!isWeekend(current2025-01-27)) {110        count→ 1++;111    }
    All 10 passes — pass 1 is the card above
    passcurrentcount
    12025-01-270 1
    22025-01-281 2
    32025-01-292 3
    42025-01-303 4
    52025-01-314 5
    62025-02-035 6
    72025-02-046 7
    82025-02-057 8
    92025-02-068 9
    102025-02-079 10
  16. current ← 2025-01-28

    111    }112    current→ 2025-01-28 = current.plusDays(1);113}
  17. current ← 2025-01-29

    111    }112    current→ 2025-01-29 = current.plusDays(1);113}
  18. current ← 2025-01-30

    111    }112    current→ 2025-01-30 = current.plusDays(1);113}
  19. current ← 2025-01-31

    111    }112    current→ 2025-01-31 = current.plusDays(1);113}
  20. current ← 2025-02-01

    111    }112    current→ 2025-02-01 = current.plusDays(1);113}
  21. current ← 2025-02-02

    111    }112    current→ 2025-02-02 = current.plusDays(1);113}
  22. current ← 2025-02-03

    111    }112    current→ 2025-02-03 = current.plusDays(1);113}
  23. current ← 2025-02-04

    111    }112    current→ 2025-02-04 = current.plusDays(1);113}
  24. current ← 2025-02-05

    111    }112    current→ 2025-02-05 = current.plusDays(1);113}
  25. current ← 2025-02-06

    111    }112    current→ 2025-02-06 = current.plusDays(1);113}
  26. current ← 2025-02-07

    111    }112    current→ 2025-02-07 = current.plusDays(1);113}
  27. current ← 2025-02-08

    111    }112    current→ 2025-02-08 = current.plusDays(1);113}
  28. return count;

    113    }114    return count10;115}
  29. System.out.println(" Business days from " + start + " to " + end + ": …

    59LocalDate end = LocalDate.of(2025, 2, 7);    // Friday60System.out.println("\nBusiness days from " + start2025-01-27 + " to " + end2025-02-07 + ": " + 61                  countBusinessDays(start2025-01-27, end2025-02-07));6263// Weekend check64System.out.println("\nWeekend check:");65LocalDate[] testDates = {66    LocalDate.of(2025, 1, 27),  // Monday67    LocalDate.of(2025, 2, 1),   // Saturday68    LocalDate.of(2025, 2, 2)    // Sunday69};
    output
    Business days from 2025-01-27 to 2025-02-07: 10
    
    Weekend check:
  30. for (LocalDate d : testDates)

    pass 1 of 3
    71for (LocalDate d2025-01-27 : testDates) {72    System.out.println(d2025-01-27 + " (" + d.getDayOfWeek() + "): " + 73                     (isWeekend(d2025-01-27) ? "Weekend" : "Weekday"));74}
    All 3 passes — pass 1 is the card above
    passd
    12025-01-27
    22025-02-01
    32025-02-02
  31. System.out.println(d + " (" + d.getDayOfWeek() + "): " +

    71for (LocalDate d : testDates) {72    System.out.println(d2025-01-27 + " (" + d.getDayOfWeek() + "): " + 73                     (isWeekend(d2025-01-27) ? "Weekend" : "Weekday"));74}
    output2025-01-27 (MONDAY): Weekday
  32. System.out.println(d + " (" + d.getDayOfWeek() + "): " +

    71for (LocalDate d : testDates) {72    System.out.println(d2025-02-01 + " (" + d.getDayOfWeek() + "): " + 73                     (isWeekend(d2025-02-01) ? "Weekend" : "Weekday"));74}
    output2025-02-01 (SATURDAY): Weekend
  33. today ← 2025-01-29, businessDaysToAdd ← 5

    71    for (LocalDate d : testDates) {72        System.out.println(d2025-02-02 + " (" + d.getDayOfWeek() + "): " + 73                         (isWeekend(d2025-02-02) ? "Weekend" : "Weekday"));74    }7576    // Add business days77    System.out.println("\nAdd business days:");78    LocalDate today→ 2025-01-29 = LocalDate.of(2025, 1, 29);  // Wednesday79    int businessDaysToAdd→ 5 = 5; //@businessDaysToAdd=5, 280    System.out.println("Today: " + today2025-01-29 + " (" + today.getDayOfWeek() + ")");81    System.out.println("+" + businessDaysToAdd5 + " business days: " + addBusinessDays(today2025-01-29, businessDaysToAdd));82}
    output2025-02-02 (SUNDAY): Weekend
    
    Add business days:
    Today: 2025-01-29 (WEDNESDAY)
  34. result ← 2025-01-29, added ← 0

    117public static LocalDate addBusinessDays(LocalDate date2025-01-29, int days5) {118    LocalDate result→ 2025-01-29 = date;119    int added→ 0 = 0;120    while (added < days) {
  35. result ← 2025-01-30

    pass 1 of 7
    119int added = 0;120while (added0 < days5) {121    result→ 2025-01-30 = result.plusDays(1);122    if (!isWeekend(result)) {
    All 7 passes — pass 1 is the card above
    passaddedresult
    102025-01-30
    212025-01-31
    322025-02-01
    422025-02-02
    522025-02-03
    632025-02-04
    742025-02-05
  36. added ← 1

    pass 1 of 5
    121result = result.plusDays(1);122if (!isWeekend(result2025-01-30)) {123    added→ 1++;124}
    All 5 passes — pass 1 is the card above
    passresultadded
    12025-01-300 1
    22025-01-311 2
    32025-02-032 3
    42025-02-043 4
    52025-02-054 5
  37. return result;

    125    }126    return result2025-02-05;127}
  38. System.out.println("+" + businessDaysToAdd + " business days: " + addB…

    80    System.out.println("Today: " + today + " (" + today.getDayOfWeek() + ")");81    System.out.println("+" + businessDaysToAdd5 + " business days: " + addBusinessDays(today2025-01-29, businessDaysToAdd));82}
    output+5 business days: 2025-02-05
  1. date ← 2025-02-01, dow ← SATURDAY, nextMonday ← 2025-02-03, prevFriday ← 2025-01-31

    10public static void main(String[] args) {11    LocalDate date→ 2025-02-01 = LocalDate.of(2025, 2, 1);12    System.out.println("Date: " + date2025-02-01);13    System.out.println();1415    // Get day of week16    DayOfWeek dow→ SATURDAY = date.getDayOfWeek();17    System.out.println("Day of week: " + dowSATURDAY);18    System.out.println("Day value (1=Mon, 7=Sun): " + dow.getValue());1920    // Check specific days21    System.out.println("\nCheck specific days:");22    System.out.println("Is Monday? " + (dowSATURDAY == DayOfWeek.MONDAY));23    System.out.println("Is weekend? " + (dowSATURDAY == DayOfWeek.SATURDAY || dow == DayOfWeek.SUNDAY));2425    // Next/previous specific day26    System.out.println("\nNext/previous specific day:");27    LocalDate nextMonday→ 2025-02-03 = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));28    System.out.println("Next Monday: " + nextMonday2025-02-03);29    30    LocalDate prevFriday→ 2025-01-31 = date.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));31    System.out.println("Previous Friday: " + prevFriday2025-01-31);3233    // Next or same34    LocalDate nextOrSameMon→ 2025-02-03 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));35    System.out.println("Next or same Monday: " + nextOrSameMon2025-02-03);3637    // Days in week38    System.out.println("\nAll days of week:");39    for (DayOfWeek day : DayOfWeek.values()) {
    outputDate: 2025-02-01
    Day of week: SATURDAY
    Day value (1=Mon, 7=Sun): 6
    
    Check specific days:
    Is Monday? false
    Is weekend? true
    
    Next/previous specific day:
    Next Monday: 2025-02-03
    Previous Friday: 2025-01-31
    Next or same Monday: 2025-02-03
    
    All days of week:
  2. for (DayOfWeek day : DayOfWeek.values())

    pass 1 of 7
    38System.out.println("\nAll days of week:");39for (DayOfWeek dayMONDAY : DayOfWeek.values()) {40    System.out.println(day.getValue() + ": " + dayMONDAY);41}
    output1: MONDAY
    All 7 passes — pass 1 is the card above
    passday
    1MONDAY
    2TUESDAY
    3WEDNESDAY
    4THURSDAY
    5FRIDAY
    6SATURDAY
    7SUNDAY
  3. monday ← 2025-01-27

    43// Week navigation44System.out.println("\nWeek navigation:");45LocalDate monday→ 2025-01-27 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));46System.out.println("This week:");47for (int i = 0; i < 7; i++) {
    output
    Week navigation:
    This week:
  4. d ← 2025-01-27

    pass 1 of 7
    46System.out.println("This week:");47for (int i0 = 0; i < 7; i++) {48    LocalDate d→ 2025-01-27 = monday.plusDays(i0);49    System.out.println(d.getDayOfWeek() + ": " + d2025-01-27);50}
    outputMONDAY: 2025-01-27
    All 7 passes — pass 1 is the card above
    passid
    102025-01-27
    212025-01-28
    322025-01-29
    432025-01-30
    542025-01-31
    652025-02-01
    762025-02-02
  5. System.out.println("Next business day: " + nextBusinessDay(date));

    52// Business days53System.out.println("\nBusiness days:");54System.out.println("Next business day: " + nextBusinessDay(date2025-02-01));55System.out.println("Previous business day: " + previousBusinessDay(date));
    output
    Business days:
  6. next ← 2025-02-02

    84public static LocalDate nextBusinessDay(LocalDate date2025-02-01) {85    LocalDate next→ 2025-02-02 = date.plusDays(1);86    while (isWeekend(next)) {
  7. dow ← SUNDAY

    pass 1 of 25
    100public static boolean isWeekend(LocalDate date2025-02-02) {101    DayOfWeek dow→ SUNDAY = date.getDayOfWeek();102    return dowSUNDAY == DayOfWeek.SATURDAY || dow == DayOfWeek.SUNDAY;103}
    25 passes — pass 1 is the card above
    passdatedownext
    12025-02-02SUNDAY2025-02-02 2025-02-03
    22025-02-03MONDAY
    32025-01-31FRIDAY
    42025-01-27MONDAY
    52025-01-28TUESDAY
    62025-01-29WEDNESDAY
    72025-01-30THURSDAY
    82025-01-31FRIDAY
    92025-02-01SATURDAY
    ⋯ 14 more passes ⋯
    242025-02-04TUESDAY
    252025-02-05WEDNESDAY
  8. next ← 2025-02-03

    85LocalDate next = date.plusDays(1);86while (isWeekend(next2025-02-02)) {87    next→ 2025-02-03 = next.plusDays(1);88}
  9. return next;

    88    }89    return next2025-02-03;90}
  10. System.out.println("Next business day: " + nextBusinessDay(date));

    53System.out.println("\nBusiness days:");54System.out.println("Next business day: " + nextBusinessDay(date2025-02-01));55System.out.println("Previous business day: " + previousBusinessDay(date2025-02-01));
    outputNext business day: 2025-02-03
  11. prev ← 2025-01-31

    92public static LocalDate previousBusinessDay(LocalDate date2025-02-01) {93    LocalDate prev→ 2025-01-31 = date.minusDays(1);94    while (isWeekend(prev)) {
  12. return prev;

    96    }97    return prev2025-01-31;98}
  13. start ← 2025-01-27, end ← 2025-02-07

    54System.out.println("Next business day: " + nextBusinessDay(date));55System.out.println("Previous business day: " + previousBusinessDay(date2025-02-01));5657// Count business days58LocalDate start→ 2025-01-27 = LocalDate.of(2025, 1, 27);  // Monday59LocalDate end→ 2025-02-07 = LocalDate.of(2025, 2, 7);    // Friday60System.out.println("\nBusiness days from " + start2025-01-27 + " to " + end2025-02-07 + ": " + 61                  countBusinessDays(start2025-01-27, end2025-02-07));
    outputPrevious business day: 2025-01-31
  14. count ← 0, current ← 2025-01-27

    105public static long countBusinessDays(LocalDate start2025-01-27, LocalDate end2025-02-07) {106    long count→ 0 = 0;107    LocalDate current→ 2025-01-27 = start;108    while (!current.isAfter(end)) {
  15. while (!current.isAfter(end))

    pass 1 of 12
    107LocalDate current = start;108while (!current.isAfter(end2025-02-07)) {109    if (!isWeekend(current)) {
  16. count ← 1

    pass 1 of 10
    108while (!current.isAfter(end)) {109    if (!isWeekend(current2025-01-27)) {110        count→ 1++;111    }
    All 10 passes — pass 1 is the card above
    passcurrentcount
    12025-01-270 1
    22025-01-281 2
    32025-01-292 3
    42025-01-303 4
    52025-01-314 5
    62025-02-035 6
    72025-02-046 7
    82025-02-057 8
    92025-02-068 9
    102025-02-079 10
  17. current ← 2025-01-28

    111    }112    current→ 2025-01-28 = current.plusDays(1);113}
  18. current ← 2025-01-29

    111    }112    current→ 2025-01-29 = current.plusDays(1);113}
  19. current ← 2025-01-30

    111    }112    current→ 2025-01-30 = current.plusDays(1);113}
  20. current ← 2025-01-31

    111    }112    current→ 2025-01-31 = current.plusDays(1);113}
  21. current ← 2025-02-01

    111    }112    current→ 2025-02-01 = current.plusDays(1);113}
  22. current ← 2025-02-02

    111    }112    current→ 2025-02-02 = current.plusDays(1);113}
  23. current ← 2025-02-03

    111    }112    current→ 2025-02-03 = current.plusDays(1);113}
  24. current ← 2025-02-04

    111    }112    current→ 2025-02-04 = current.plusDays(1);113}
  25. current ← 2025-02-05

    111    }112    current→ 2025-02-05 = current.plusDays(1);113}
  26. current ← 2025-02-06

    111    }112    current→ 2025-02-06 = current.plusDays(1);113}
  27. current ← 2025-02-07

    111    }112    current→ 2025-02-07 = current.plusDays(1);113}
  28. current ← 2025-02-08

    111    }112    current→ 2025-02-08 = current.plusDays(1);113}
  29. return count;

    113    }114    return count10;115}
  30. System.out.println(" Business days from " + start + " to " + end + ": …

    59LocalDate end = LocalDate.of(2025, 2, 7);    // Friday60System.out.println("\nBusiness days from " + start2025-01-27 + " to " + end2025-02-07 + ": " + 61                  countBusinessDays(start2025-01-27, end2025-02-07));6263// Weekend check64System.out.println("\nWeekend check:");65LocalDate[] testDates = {66    LocalDate.of(2025, 1, 27),  // Monday67    LocalDate.of(2025, 2, 1),   // Saturday68    LocalDate.of(2025, 2, 2)    // Sunday69};
    output
    Business days from 2025-01-27 to 2025-02-07: 10
    
    Weekend check:
  31. for (LocalDate d : testDates)

    pass 1 of 3
    71for (LocalDate d2025-01-27 : testDates) {72    System.out.println(d2025-01-27 + " (" + d.getDayOfWeek() + "): " + 73                     (isWeekend(d2025-01-27) ? "Weekend" : "Weekday"));74}
    All 3 passes — pass 1 is the card above
    passd
    12025-01-27
    22025-02-01
    32025-02-02
  32. System.out.println(d + " (" + d.getDayOfWeek() + "): " +

    71for (LocalDate d : testDates) {72    System.out.println(d2025-01-27 + " (" + d.getDayOfWeek() + "): " + 73                     (isWeekend(d2025-01-27) ? "Weekend" : "Weekday"));74}
    output2025-01-27 (MONDAY): Weekday
  33. System.out.println(d + " (" + d.getDayOfWeek() + "): " +

    71for (LocalDate d : testDates) {72    System.out.println(d2025-02-01 + " (" + d.getDayOfWeek() + "): " + 73                     (isWeekend(d2025-02-01) ? "Weekend" : "Weekday"));74}
    output2025-02-01 (SATURDAY): Weekend
  34. today ← 2025-01-29, businessDaysToAdd ← 5

    71    for (LocalDate d : testDates) {72        System.out.println(d2025-02-02 + " (" + d.getDayOfWeek() + "): " + 73                         (isWeekend(d2025-02-02) ? "Weekend" : "Weekday"));74    }7576    // Add business days77    System.out.println("\nAdd business days:");78    LocalDate today→ 2025-01-29 = LocalDate.of(2025, 1, 29);  // Wednesday79    int businessDaysToAdd→ 5 = 5;80    System.out.println("Today: " + today2025-01-29 + " (" + today.getDayOfWeek() + ")");81    System.out.println("+" + businessDaysToAdd5 + " business days: " + addBusinessDays(today2025-01-29, businessDaysToAdd));82}
    output2025-02-02 (SUNDAY): Weekend
    
    Add business days:
    Today: 2025-01-29 (WEDNESDAY)
  35. result ← 2025-01-29, added ← 0

    117public static LocalDate addBusinessDays(LocalDate date2025-01-29, int days5) {118    LocalDate result→ 2025-01-29 = date;119    int added→ 0 = 0;120    while (added < days) {
  36. result ← 2025-01-30

    pass 1 of 7
    119int added = 0;120while (added0 < days5) {121    result→ 2025-01-30 = result.plusDays(1);122    if (!isWeekend(result)) {
    All 7 passes — pass 1 is the card above
    passaddedresult
    102025-01-30
    212025-01-31
    322025-02-01
    422025-02-02
    522025-02-03
    632025-02-04
    742025-02-05
  37. added ← 1

    pass 1 of 5
    121result = result.plusDays(1);122if (!isWeekend(result2025-01-30)) {123    added→ 1++;124}
    All 5 passes — pass 1 is the card above
    passresultadded
    12025-01-300 1
    22025-01-311 2
    32025-02-032 3
    42025-02-043 4
    52025-02-054 5
  38. return result;

    125    }126    return result2025-02-05;127}
  39. System.out.println("+" + businessDaysToAdd + " business days: " + addB…

    80    System.out.println("Today: " + today + " (" + today.getDayOfWeek() + ")");81    System.out.println("+" + businessDaysToAdd5 + " business days: " + addBusinessDays(today2025-01-29, businessDaysToAdd));82}
    output+5 business days: 2025-02-05
  1. date ← 2025-01-29, dow ← WEDNESDAY, nextMonday ← 2025-02-03, prevFriday ← 2025-01-24

    10public static void main(String[] args) {11    LocalDate date→ 2025-01-29 = LocalDate.of(2025, 1, 29);12    System.out.println("Date: " + date2025-01-29);13    System.out.println();1415    // Get day of week16    DayOfWeek dow→ WEDNESDAY = date.getDayOfWeek();17    System.out.println("Day of week: " + dowWEDNESDAY);18    System.out.println("Day value (1=Mon, 7=Sun): " + dow.getValue());1920    // Check specific days21    System.out.println("\nCheck specific days:");22    System.out.println("Is Monday? " + (dowWEDNESDAY == DayOfWeek.MONDAY));23    System.out.println("Is weekend? " + (dowWEDNESDAY == DayOfWeek.SATURDAY || dow == DayOfWeek.SUNDAY));2425    // Next/previous specific day26    System.out.println("\nNext/previous specific day:");27    LocalDate nextMonday→ 2025-02-03 = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));28    System.out.println("Next Monday: " + nextMonday2025-02-03);29    30    LocalDate prevFriday→ 2025-01-24 = date.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));31    System.out.println("Previous Friday: " + prevFriday2025-01-24);3233    // Next or same34    LocalDate nextOrSameMon→ 2025-02-03 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));35    System.out.println("Next or same Monday: " + nextOrSameMon2025-02-03);3637    // Days in week38    System.out.println("\nAll days of week:");39    for (DayOfWeek day : DayOfWeek.values()) {
    outputDate: 2025-01-29
    Day of week: WEDNESDAY
    Day value (1=Mon, 7=Sun): 3
    
    Check specific days:
    Is Monday? false
    Is weekend? false
    
    Next/previous specific day:
    Next Monday: 2025-02-03
    Previous Friday: 2025-01-24
    Next or same Monday: 2025-02-03
    
    All days of week:
  2. for (DayOfWeek day : DayOfWeek.values())

    pass 1 of 7
    38System.out.println("\nAll days of week:");39for (DayOfWeek dayMONDAY : DayOfWeek.values()) {40    System.out.println(day.getValue() + ": " + dayMONDAY);41}
    output1: MONDAY
    All 7 passes — pass 1 is the card above
    passday
    1MONDAY
    2TUESDAY
    3WEDNESDAY
    4THURSDAY
    5FRIDAY
    6SATURDAY
    7SUNDAY
  3. monday ← 2025-01-27

    43// Week navigation44System.out.println("\nWeek navigation:");45LocalDate monday→ 2025-01-27 = date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));46System.out.println("This week:");47for (int i = 0; i < 7; i++) {
    output
    Week navigation:
    This week:
  4. d ← 2025-01-27

    pass 1 of 7
    46System.out.println("This week:");47for (int i0 = 0; i < 7; i++) {48    LocalDate d→ 2025-01-27 = monday.plusDays(i0);49    System.out.println(d.getDayOfWeek() + ": " + d2025-01-27);50}
    outputMONDAY: 2025-01-27
    All 7 passes — pass 1 is the card above
    passid
    102025-01-27
    212025-01-28
    322025-01-29
    432025-01-30
    542025-01-31
    652025-02-01
    762025-02-02
  5. System.out.println("Next business day: " + nextBusinessDay(date));

    52// Business days53System.out.println("\nBusiness days:");54System.out.println("Next business day: " + nextBusinessDay(date2025-01-29));55System.out.println("Previous business day: " + previousBusinessDay(date));
    output
    Business days:
  6. next ← 2025-01-30

    84public static LocalDate nextBusinessDay(LocalDate date2025-01-29) {85    LocalDate next→ 2025-01-30 = date.plusDays(1);86    while (isWeekend(next)) {
  7. dow ← THURSDAY

    pass 1 of 19
    100public static boolean isWeekend(LocalDate date2025-01-30) {101    DayOfWeek dow→ THURSDAY = date.getDayOfWeek();102    return dowTHURSDAY == DayOfWeek.SATURDAY || dow == DayOfWeek.SUNDAY;103}
    19 passes — pass 1 is the card above
    passdatedaysdowaddedresult
    12025-01-30THURSDAY
    22025-01-28TUESDAY
    32025-01-27MONDAY
    42025-01-28TUESDAY
    52025-01-29WEDNESDAY
    62025-01-30THURSDAY
    72025-01-31FRIDAY
    82025-02-01SATURDAY
    92025-02-02SUNDAY
    ⋯ 8 more passes ⋯
    182025-01-302THURSDAY0 12025-01-31
    192025-01-31FRIDAY1 22025-01-31
  8. return next;

    88    }89    return next2025-01-30;90}
  9. System.out.println("Next business day: " + nextBusinessDay(date));

    53System.out.println("\nBusiness days:");54System.out.println("Next business day: " + nextBusinessDay(date2025-01-29));55System.out.println("Previous business day: " + previousBusinessDay(date2025-01-29));
    outputNext business day: 2025-01-30
  10. prev ← 2025-01-28

    92public static LocalDate previousBusinessDay(LocalDate date2025-01-29) {93    LocalDate prev→ 2025-01-28 = date.minusDays(1);94    while (isWeekend(prev)) {
  11. return prev;

    96    }97    return prev2025-01-28;98}
  12. start ← 2025-01-27, end ← 2025-02-07

    54System.out.println("Next business day: " + nextBusinessDay(date));55System.out.println("Previous business day: " + previousBusinessDay(date2025-01-29));5657// Count business days58LocalDate start→ 2025-01-27 = LocalDate.of(2025, 1, 27);  // Monday59LocalDate end→ 2025-02-07 = LocalDate.of(2025, 2, 7);    // Friday60System.out.println("\nBusiness days from " + start2025-01-27 + " to " + end2025-02-07 + ": " + 61                  countBusinessDays(start2025-01-27, end2025-02-07));
    outputPrevious business day: 2025-01-28
  13. count ← 0, current ← 2025-01-27

    105public static long countBusinessDays(LocalDate start2025-01-27, LocalDate end2025-02-07) {106    long count→ 0 = 0;107    LocalDate current→ 2025-01-27 = start;108    while (!current.isAfter(end)) {
  14. while (!current.isAfter(end))

    pass 1 of 12
    107LocalDate current = start;108while (!current.isAfter(end2025-02-07)) {109    if (!isWeekend(current)) {
  15. count ← 1

    pass 1 of 10
    108while (!current.isAfter(end)) {109    if (!isWeekend(current2025-01-27)) {110        count→ 1++;111    }
    All 10 passes — pass 1 is the card above
    passcurrentcount
    12025-01-270 1
    22025-01-281 2
    32025-01-292 3
    42025-01-303 4
    52025-01-314 5
    62025-02-035 6
    72025-02-046 7
    82025-02-057 8
    92025-02-068 9
    102025-02-079 10
  16. current ← 2025-01-28

    111    }112    current→ 2025-01-28 = current.plusDays(1);113}
  17. current ← 2025-01-29

    111    }112    current→ 2025-01-29 = current.plusDays(1);113}
  18. current ← 2025-01-30

    111    }112    current→ 2025-01-30 = current.plusDays(1);113}
  19. current ← 2025-01-31

    111    }112    current→ 2025-01-31 = current.plusDays(1);113}
  20. current ← 2025-02-01

    111    }112    current→ 2025-02-01 = current.plusDays(1);113}
  21. current ← 2025-02-02

    111    }112    current→ 2025-02-02 = current.plusDays(1);113}
  22. current ← 2025-02-03

    111    }112    current→ 2025-02-03 = current.plusDays(1);113}
  23. current ← 2025-02-04

    111    }112    current→ 2025-02-04 = current.plusDays(1);113}
  24. current ← 2025-02-05

    111    }112    current→ 2025-02-05 = current.plusDays(1);113}
  25. current ← 2025-02-06

    111    }112    current→ 2025-02-06 = current.plusDays(1);113}
  26. current ← 2025-02-07

    111    }112    current→ 2025-02-07 = current.plusDays(1);113}
  27. current ← 2025-02-08

    111    }112    current→ 2025-02-08 = current.plusDays(1);113}
  28. return count;

    113    }114    return count10;115}
  29. System.out.println(" Business days from " + start + " to " + end + ": …

    59LocalDate end = LocalDate.of(2025, 2, 7);    // Friday60System.out.println("\nBusiness days from " + start2025-01-27 + " to " + end2025-02-07 + ": " + 61                  countBusinessDays(start2025-01-27, end2025-02-07));6263// Weekend check64System.out.println("\nWeekend check:");65LocalDate[] testDates = {66    LocalDate.of(2025, 1, 27),  // Monday67    LocalDate.of(2025, 2, 1),   // Saturday68    LocalDate.of(2025, 2, 2)    // Sunday69};
    output
    Business days from 2025-01-27 to 2025-02-07: 10
    
    Weekend check:
  30. for (LocalDate d : testDates)

    pass 1 of 3
    71for (LocalDate d2025-01-27 : testDates) {72    System.out.println(d2025-01-27 + " (" + d.getDayOfWeek() + "): " + 73                     (isWeekend(d2025-01-27) ? "Weekend" : "Weekday"));74}
    All 3 passes — pass 1 is the card above
    passd
    12025-01-27
    22025-02-01
    32025-02-02
  31. System.out.println(d + " (" + d.getDayOfWeek() + "): " +

    71for (LocalDate d : testDates) {72    System.out.println(d2025-01-27 + " (" + d.getDayOfWeek() + "): " + 73                     (isWeekend(d2025-01-27) ? "Weekend" : "Weekday"));74}
    output2025-01-27 (MONDAY): Weekday
  32. System.out.println(d + " (" + d.getDayOfWeek() + "): " +

    71for (LocalDate d : testDates) {72    System.out.println(d2025-02-01 + " (" + d.getDayOfWeek() + "): " + 73                     (isWeekend(d2025-02-01) ? "Weekend" : "Weekday"));74}
    output2025-02-01 (SATURDAY): Weekend
  33. today ← 2025-01-29, businessDaysToAdd ← 2

    71    for (LocalDate d : testDates) {72        System.out.println(d2025-02-02 + " (" + d.getDayOfWeek() + "): " + 73                         (isWeekend(d2025-02-02) ? "Weekend" : "Weekday"));74    }7576    // Add business days77    System.out.println("\nAdd business days:");78    LocalDate today→ 2025-01-29 = LocalDate.of(2025, 1, 29);  // Wednesday79    int businessDaysToAdd→ 2 = 2;80    System.out.println("Today: " + today2025-01-29 + " (" + today.getDayOfWeek() + ")");81    System.out.println("+" + businessDaysToAdd2 + " business days: " + addBusinessDays(today2025-01-29, businessDaysToAdd));82}
    output2025-02-02 (SUNDAY): Weekend
    
    Add business days:
    Today: 2025-01-29 (WEDNESDAY)
  34. result ← 2025-01-29, added ← 0

    117public static LocalDate addBusinessDays(LocalDate date2025-01-29, int days2) {118    LocalDate result→ 2025-01-29 = date;119    int added→ 0 = 0;120    while (added < days) {
  35. result ← 2025-01-30

    pass 1 of 2
    119int added = 0;120while (added0 < days2) {121    result→ 2025-01-30 = result.plusDays(1);122    if (!isWeekend(result)) {
  36. added ← 1

    pass 1 of 2
    121result = result.plusDays(1);122if (!isWeekend(result2025-01-30)) {123    added→ 1++;124}
  37. result ← 2025-01-31

    pass 2 of 2
    119int added = 0;120while (added1 < days2) {121    result→ 2025-01-31 = result.plusDays(1);122    if (!isWeekend(result)) {
  38. added ← 2

    pass 2 of 2
    121result = result.plusDays(1);122if (!isWeekend(result2025-01-31)) {123    added→ 2++;124}
  39. return result;

    125    }126    return result2025-01-31;127}
  40. System.out.println("+" + businessDaysToAdd + " business days: " + addB…

    80    System.out.println("Today: " + today + " (" + today.getDayOfWeek() + ")");81    System.out.println("+" + businessDaysToAdd2 + " business days: " + addBusinessDays(today2025-01-29, businessDaysToAdd));82}
    output+2 business days: 2025-01-31

Month Operations

date
MonthCalc.java
Replay: real traced execution (multi-file project)
// Month operations

import java.time.LocalDate;
import java.time.Month;
import java.time.YearMonth;
import java.time.temporal.TemporalAdjusters;

public class MonthCalc {

    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2025, 1, 29);
        System.out.println("Date: " + date);
        System.out.println();

        // Get month
        Month month = date.getMonth();
        System.out.println("Month: " + month);
        System.out.println("Month value: " + month.getValue());
        System.out.println("Month length: " + month.length(date.isLeapYear()));

        // Month navigation
        System.out.println("\nMonth navigation:");
        System.out.println("Next month: " + month.plus(1));
        System.out.println("Previous month: " + month.minus(1));

        // All months
        System.out.println("\nAll months:");
        for (Month m : Month.values()) {
            System.out.println(m.getValue() + ": " + m);
        }

        // First/last day of month
        System.out.println("\nFirst/last day of month:");
        LocalDate firstDay = date.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate lastDay = date.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println("First day: " + firstDay);
        System.out.println("Last day: " + lastDay);

        // YearMonth
        System.out.println("\nYearMonth:");
        YearMonth ym = YearMonth.of(2025, 1);
        System.out.println("YearMonth: " + ym);
        System.out.println("Length: " + ym.lengthOfMonth() + " days");
        System.out.println("Is leap year: " + ym.isLeapYear());
        System.out.println("First day: " + ym.atDay(1));
        System.out.println("Last day: " + ym.atEndOfMonth());

        // Month lengths
        System.out.println("\nMonth lengths (2025):");
        for (Month m : Month.values()) {
            YearMonth yearMonth = YearMonth.of(2025, m);
            System.out.println(m + ": " + yearMonth.lengthOfMonth() + " days");
        }

        // Leap year comparison
        System.out.println("\nFebruary length:");
        System.out.println("2024 (leap): " + YearMonth.of(2024, 2).lengthOfMonth());
        System.out.println("2025 (not): " + YearMonth.of(2025, 2).lengthOfMonth());

        // Month range
        System.out.println("\nMonth range:");
        LocalDate start = LocalDate.of(2025, 1, 15);
        LocalDate end = LocalDate.of(2025, 3, 20);
        System.out.println("From " + start + " to " + end);

        YearMonth current = YearMonth.from(start);
        YearMonth last = YearMonth.from(end);
        while (!current.isAfter(last)) {
            System.out.println(current + " (" + current.lengthOfMonth() + " days)");
            current = current.plusMonths(1);
        }

        // Add months with overflow
        System.out.println("\nAdd months (handle overflow):");
        LocalDate jan31 = LocalDate.of(2025, 1, 31);
        System.out.println("Jan 31: " + jan31);
        System.out.println("+1 month: " + jan31.plusMonths(1));  // Feb 28
        System.out.println("+2 months: " + jan31.plusMonths(2)); // Mar 31

        // Practical examples
        System.out.println("\nPractical examples:");

        // Quarterly dates
        LocalDate q1End = LocalDate.of(2025, 3, 31);
        LocalDate q2End = q1End.plusMonths(3);
        LocalDate q3End = q2End.plusMonths(3);
        LocalDate q4End = q3End.plusMonths(3);
        System.out.println("Q1 end: " + q1End);
        System.out.println("Q2 end: " + q2End);
        System.out.println("Q3 end: " + q3End);
        System.out.println("Q4 end: " + q4End);

        // Monthly billing dates
        System.out.println("\nMonthly billing dates (starting " + date + "):");
        for (int i = 0; i < 6; i++) {
            LocalDate billing = date.plusMonths(i);
            System.out.println("Month " + i + ": " + billing);
        }

        // End of each month
        System.out.println("\nEnd of each month (2025):");
        for (int m = 1; m <= 12; m++) {
            LocalDate monthEnd = YearMonth.of(2025, m).atEndOfMonth();
            System.out.println(monthEnd.getMonth() + ": " + monthEnd);
        }
    }

}
// Month operations

import java.time.LocalDate;
import java.time.Month;
import java.time.YearMonth;
import java.time.temporal.TemporalAdjusters;

public class MonthCalc {

    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2024, 2, 29);
        System.out.println("Date: " + date);
        System.out.println();

        // Get month
        Month month = date.getMonth();
        System.out.println("Month: " + month);
        System.out.println("Month value: " + month.getValue());
        System.out.println("Month length: " + month.length(date.isLeapYear()));

        // Month navigation
        System.out.println("\nMonth navigation:");
        System.out.println("Next month: " + month.plus(1));
        System.out.println("Previous month: " + month.minus(1));

        // All months
        System.out.println("\nAll months:");
        for (Month m : Month.values()) {
            System.out.println(m.getValue() + ": " + m);
        }

        // First/last day of month
        System.out.println("\nFirst/last day of month:");
        LocalDate firstDay = date.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate lastDay = date.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println("First day: " + firstDay);
        System.out.println("Last day: " + lastDay);

        // YearMonth
        System.out.println("\nYearMonth:");
        YearMonth ym = YearMonth.of(2025, 1);
        System.out.println("YearMonth: " + ym);
        System.out.println("Length: " + ym.lengthOfMonth() + " days");
        System.out.println("Is leap year: " + ym.isLeapYear());
        System.out.println("First day: " + ym.atDay(1));
        System.out.println("Last day: " + ym.atEndOfMonth());

        // Month lengths
        System.out.println("\nMonth lengths (2025):");
        for (Month m : Month.values()) {
            YearMonth yearMonth = YearMonth.of(2025, m);
            System.out.println(m + ": " + yearMonth.lengthOfMonth() + " days");
        }

        // Leap year comparison
        System.out.println("\nFebruary length:");
        System.out.println("2024 (leap): " + YearMonth.of(2024, 2).lengthOfMonth());
        System.out.println("2025 (not): " + YearMonth.of(2025, 2).lengthOfMonth());

        // Month range
        System.out.println("\nMonth range:");
        LocalDate start = LocalDate.of(2025, 1, 15);
        LocalDate end = LocalDate.of(2025, 3, 20);
        System.out.println("From " + start + " to " + end);

        YearMonth current = YearMonth.from(start);
        YearMonth last = YearMonth.from(end);
        while (!current.isAfter(last)) {
            System.out.println(current + " (" + current.lengthOfMonth() + " days)");
            current = current.plusMonths(1);
        }

        // Add months with overflow
        System.out.println("\nAdd months (handle overflow):");
        LocalDate jan31 = LocalDate.of(2025, 1, 31);
        System.out.println("Jan 31: " + jan31);
        System.out.println("+1 month: " + jan31.plusMonths(1));  // Feb 28
        System.out.println("+2 months: " + jan31.plusMonths(2)); // Mar 31

        // Practical examples
        System.out.println("\nPractical examples:");

        // Quarterly dates
        LocalDate q1End = LocalDate.of(2025, 3, 31);
        LocalDate q2End = q1End.plusMonths(3);
        LocalDate q3End = q2End.plusMonths(3);
        LocalDate q4End = q3End.plusMonths(3);
        System.out.println("Q1 end: " + q1End);
        System.out.println("Q2 end: " + q2End);
        System.out.println("Q3 end: " + q3End);
        System.out.println("Q4 end: " + q4End);

        // Monthly billing dates
        System.out.println("\nMonthly billing dates (starting " + date + "):");
        for (int i = 0; i < 6; i++) {
            LocalDate billing = date.plusMonths(i);
            System.out.println("Month " + i + ": " + billing);
        }

        // End of each month
        System.out.println("\nEnd of each month (2025):");
        for (int m = 1; m <= 12; m++) {
            LocalDate monthEnd = YearMonth.of(2025, m).atEndOfMonth();
            System.out.println(monthEnd.getMonth() + ": " + monthEnd);
        }
    }

}
  1. date ← 2025-01-29, month ← JANUARY

    10public static void main(String[] args) {11    LocalDate date→ 2025-01-29 = LocalDate.of(2025, 1, 29); //@date=LocalDate.of(2025, 1, 29), LocalDate.of(2024, 2, 29)12    System.out.println("Date: " + date2025-01-29);13    System.out.println();1415    // Get month16    Month month→ JANUARY = date.getMonth();17    System.out.println("Month: " + monthJANUARY);18    System.out.println("Month value: " + month.getValue());19    System.out.println("Month length: " + month.length(date.isLeapYear()));2021    // Month navigation22    System.out.println("\nMonth navigation:");23    System.out.println("Next month: " + month.plus(1));24    System.out.println("Previous month: " + month.minus(1));2526    // All months27    System.out.println("\nAll months:");28    for (Month m : Month.values()) {
    outputDate: 2025-01-29
    Month: JANUARY
    Month value: 1
    Month length: 31
    
    Month navigation:
    Next month: FEBRUARY
    Previous month: DECEMBER
    
    All months:
  2. for (Month m : Month.values())

    pass 1 of 12
    27System.out.println("\nAll months:");28for (Month mJANUARY : Month.values()) {29    System.out.println(m.getValue() + ": " + mJANUARY);30}
    output1: JANUARY
    All 12 passes — pass 1 is the card above
    passm
    1JANUARY
    2FEBRUARY
    3MARCH
    4APRIL
    5MAY
    6JUNE
    7JULY
    8AUGUST
    9SEPTEMBER
    10OCTOBER
    11NOVEMBER
    12DECEMBER
  3. firstDay ← 2025-01-01, lastDay ← 2025-01-31, ym ← 2025-01

    32// First/last day of month33System.out.println("\nFirst/last day of month:");34LocalDate firstDay→ 2025-01-01 = date.with(TemporalAdjusters.firstDayOfMonth());35LocalDate lastDay→ 2025-01-31 = date.with(TemporalAdjusters.lastDayOfMonth());36System.out.println("First day: " + firstDay2025-01-01);37System.out.println("Last day: " + lastDay2025-01-31);3839// YearMonth40System.out.println("\nYearMonth:");41YearMonth ym→ 2025-01 = YearMonth.of(2025, 1);42System.out.println("YearMonth: " + ym2025-01);43System.out.println("Length: " + ym.lengthOfMonth() + " days");44System.out.println("Is leap year: " + ym.isLeapYear());45System.out.println("First day: " + ym.atDay(1));46System.out.println("Last day: " + ym.atEndOfMonth());4748// Month lengths49System.out.println("\nMonth lengths (2025):");50for (Month m : Month.values()) {
    output
    First/last day of month:
    First day: 2025-01-01
    Last day: 2025-01-31
    
    YearMonth:
    YearMonth: 2025-01
    Length: 31 days
    Is leap year: false
    First day: 2025-01-01
    Last day: 2025-01-31
    
    Month lengths (2025):
  4. yearMonth ← 2025-01

    pass 1 of 12
    49System.out.println("\nMonth lengths (2025):");50for (Month mJANUARY : Month.values()) {51    YearMonth yearMonth→ 2025-01 = YearMonth.of(2025, mJANUARY);52    System.out.println(mJANUARY + ": " + yearMonth.lengthOfMonth() + " days");53}
    outputJANUARY: 31 days
    All 12 passes — pass 1 is the card above
    passmyearMonth
    1JANUARY2025-01
    2FEBRUARY2025-02
    3MARCH2025-03
    4APRIL2025-04
    5MAY2025-05
    6JUNE2025-06
    7JULY2025-07
    8AUGUST2025-08
    9SEPTEMBER2025-09
    10OCTOBER2025-10
    11NOVEMBER2025-11
    12DECEMBER2025-12
  5. start ← 2025-01-15, end ← 2025-03-20, current ← 2025-01, last ← 2025-03

    55// Leap year comparison56System.out.println("\nFebruary length:");57System.out.println("2024 (leap): " + YearMonth.of(2024, 2).lengthOfMonth());58System.out.println("2025 (not): " + YearMonth.of(2025, 2).lengthOfMonth());5960// Month range61System.out.println("\nMonth range:");62LocalDate start→ 2025-01-15 = LocalDate.of(2025, 1, 15);63LocalDate end→ 2025-03-20 = LocalDate.of(2025, 3, 20);64System.out.println("From " + start2025-01-15 + " to " + end2025-03-20);6566YearMonth current→ 2025-01 = YearMonth.from(start2025-01-15);67YearMonth last→ 2025-03 = YearMonth.from(end2025-03-20);68while (!current.isAfter(last)) {
    output
    February length:
    2024 (leap): 29
    2025 (not): 28
    
    Month range:
    From 2025-01-15 to 2025-03-20
  6. current ← 2025-02

    pass 1 of 3
    67YearMonth last = YearMonth.from(end);68while (!current.isAfter(last2025-03)) {69    System.out.println(current2025-01 + " (" + current.lengthOfMonth() + " days)");70    current→ 2025-02 = current.plusMonths(1);71}
    output2025-01 (31 days)
    All 3 passes — pass 1 is the card above
    passcurrent
    12025-01 2025-02
    22025-02 2025-03
    32025-03 2025-04
  7. jan31 ← 2025-01-31, q1End ← 2025-03-31, q2End ← 2025-06-30, q3End ← 2025-09-30

    73// Add months with overflow74System.out.println("\nAdd months (handle overflow):");75LocalDate jan31→ 2025-01-31 = LocalDate.of(2025, 1, 31);76System.out.println("Jan 31: " + jan312025-01-31);77System.out.println("+1 month: " + jan31.plusMonths(1));  // Feb 2878System.out.println("+2 months: " + jan31.plusMonths(2)); // Mar 317980// Practical examples81System.out.println("\nPractical examples:");8283// Quarterly dates84LocalDate q1End→ 2025-03-31 = LocalDate.of(2025, 3, 31);85LocalDate q2End→ 2025-06-30 = q1End.plusMonths(3);86LocalDate q3End→ 2025-09-30 = q2End.plusMonths(3);87LocalDate q4End→ 2025-12-30 = q3End.plusMonths(3);88System.out.println("Q1 end: " + q1End2025-03-31);89System.out.println("Q2 end: " + q2End2025-06-30);90System.out.println("Q3 end: " + q3End2025-09-30);91System.out.println("Q4 end: " + q4End2025-12-30);9293// Monthly billing dates94System.out.println("\nMonthly billing dates (starting " + date2025-01-29 + "):");95for (int i = 0; i < 6; i++) {
    output
    Add months (handle overflow):
    Jan 31: 2025-01-31
    +1 month: 2025-02-28
    +2 months: 2025-03-31
    
    Practical examples:
    Q1 end: 2025-03-31
    Q2 end: 2025-06-30
    Q3 end: 2025-09-30
    Q4 end: 2025-12-30
    
    Monthly billing dates (starting 2025-01-29):
  8. billing ← 2025-01-29

    pass 1 of 6
    94System.out.println("\nMonthly billing dates (starting " + date + "):");95for (int i0 = 0; i < 6; i++) {96    LocalDate billing→ 2025-01-29 = date.plusMonths(i0);97    System.out.println("Month " + i0 + ": " + billing2025-01-29);98}
    outputMonth 0: 2025-01-29
    All 6 passes — pass 1 is the card above
    passibilling
    102025-01-29
    212025-02-28
    322025-03-29
    432025-04-29
    542025-05-29
    652025-06-29
  9. System.out.println(" End of each month (2025):");

    100// End of each month101System.out.println("\nEnd of each month (2025):");102for (int m = 1; m <= 12; m++) {
    output
    End of each month (2025):
  10. monthEnd ← 2025-01-31

    pass 1 of 12
    101System.out.println("\nEnd of each month (2025):");102for (int m1 = 1; m <= 12; m++) {103    LocalDate monthEnd→ 2025-01-31 = YearMonth.of(2025, m1).atEndOfMonth();104    System.out.println(monthEnd.getMonth() + ": " + monthEnd2025-01-31);105}
    outputJANUARY: 2025-01-31
    All 12 passes — pass 1 is the card above
    passmmonthEnd
    112025-01-31
    222025-02-28
    332025-03-31
    442025-04-30
    552025-05-31
    662025-06-30
    772025-07-31
    882025-08-31
    992025-09-30
    10102025-10-31
    11112025-11-30
    12122025-12-31
  1. date ← 2024-02-29, month ← FEBRUARY

    10public static void main(String[] args) {11    LocalDate date→ 2024-02-29 = LocalDate.of(2024, 2, 29);12    System.out.println("Date: " + date2024-02-29);13    System.out.println();1415    // Get month16    Month month→ FEBRUARY = date.getMonth();17    System.out.println("Month: " + monthFEBRUARY);18    System.out.println("Month value: " + month.getValue());19    System.out.println("Month length: " + month.length(date.isLeapYear()));2021    // Month navigation22    System.out.println("\nMonth navigation:");23    System.out.println("Next month: " + month.plus(1));24    System.out.println("Previous month: " + month.minus(1));2526    // All months27    System.out.println("\nAll months:");28    for (Month m : Month.values()) {
    outputDate: 2024-02-29
    Month: FEBRUARY
    Month value: 2
    Month length: 29
    
    Month navigation:
    Next month: MARCH
    Previous month: JANUARY
    
    All months:
  2. for (Month m : Month.values())

    pass 1 of 12
    27System.out.println("\nAll months:");28for (Month mJANUARY : Month.values()) {29    System.out.println(m.getValue() + ": " + mJANUARY);30}
    output1: JANUARY
    All 12 passes — pass 1 is the card above
    passm
    1JANUARY
    2FEBRUARY
    3MARCH
    4APRIL
    5MAY
    6JUNE
    7JULY
    8AUGUST
    9SEPTEMBER
    10OCTOBER
    11NOVEMBER
    12DECEMBER
  3. firstDay ← 2024-02-01, lastDay ← 2024-02-29, ym ← 2025-01

    32// First/last day of month33System.out.println("\nFirst/last day of month:");34LocalDate firstDay→ 2024-02-01 = date.with(TemporalAdjusters.firstDayOfMonth());35LocalDate lastDay→ 2024-02-29 = date.with(TemporalAdjusters.lastDayOfMonth());36System.out.println("First day: " + firstDay2024-02-01);37System.out.println("Last day: " + lastDay2024-02-29);3839// YearMonth40System.out.println("\nYearMonth:");41YearMonth ym→ 2025-01 = YearMonth.of(2025, 1);42System.out.println("YearMonth: " + ym2025-01);43System.out.println("Length: " + ym.lengthOfMonth() + " days");44System.out.println("Is leap year: " + ym.isLeapYear());45System.out.println("First day: " + ym.atDay(1));46System.out.println("Last day: " + ym.atEndOfMonth());4748// Month lengths49System.out.println("\nMonth lengths (2025):");50for (Month m : Month.values()) {
    output
    First/last day of month:
    First day: 2024-02-01
    Last day: 2024-02-29
    
    YearMonth:
    YearMonth: 2025-01
    Length: 31 days
    Is leap year: false
    First day: 2025-01-01
    Last day: 2025-01-31
    
    Month lengths (2025):
  4. yearMonth ← 2025-01

    pass 1 of 12
    49System.out.println("\nMonth lengths (2025):");50for (Month mJANUARY : Month.values()) {51    YearMonth yearMonth→ 2025-01 = YearMonth.of(2025, mJANUARY);52    System.out.println(mJANUARY + ": " + yearMonth.lengthOfMonth() + " days");53}
    outputJANUARY: 31 days
    All 12 passes — pass 1 is the card above
    passmyearMonth
    1JANUARY2025-01
    2FEBRUARY2025-02
    3MARCH2025-03
    4APRIL2025-04
    5MAY2025-05
    6JUNE2025-06
    7JULY2025-07
    8AUGUST2025-08
    9SEPTEMBER2025-09
    10OCTOBER2025-10
    11NOVEMBER2025-11
    12DECEMBER2025-12
  5. start ← 2025-01-15, end ← 2025-03-20, current ← 2025-01, last ← 2025-03

    55// Leap year comparison56System.out.println("\nFebruary length:");57System.out.println("2024 (leap): " + YearMonth.of(2024, 2).lengthOfMonth());58System.out.println("2025 (not): " + YearMonth.of(2025, 2).lengthOfMonth());5960// Month range61System.out.println("\nMonth range:");62LocalDate start→ 2025-01-15 = LocalDate.of(2025, 1, 15);63LocalDate end→ 2025-03-20 = LocalDate.of(2025, 3, 20);64System.out.println("From " + start2025-01-15 + " to " + end2025-03-20);6566YearMonth current→ 2025-01 = YearMonth.from(start2025-01-15);67YearMonth last→ 2025-03 = YearMonth.from(end2025-03-20);68while (!current.isAfter(last)) {
    output
    February length:
    2024 (leap): 29
    2025 (not): 28
    
    Month range:
    From 2025-01-15 to 2025-03-20
  6. current ← 2025-02

    pass 1 of 3
    67YearMonth last = YearMonth.from(end);68while (!current.isAfter(last2025-03)) {69    System.out.println(current2025-01 + " (" + current.lengthOfMonth() + " days)");70    current→ 2025-02 = current.plusMonths(1);71}
    output2025-01 (31 days)
    All 3 passes — pass 1 is the card above
    passcurrent
    12025-01 2025-02
    22025-02 2025-03
    32025-03 2025-04
  7. jan31 ← 2025-01-31, q1End ← 2025-03-31, q2End ← 2025-06-30, q3End ← 2025-09-30

    73// Add months with overflow74System.out.println("\nAdd months (handle overflow):");75LocalDate jan31→ 2025-01-31 = LocalDate.of(2025, 1, 31);76System.out.println("Jan 31: " + jan312025-01-31);77System.out.println("+1 month: " + jan31.plusMonths(1));  // Feb 2878System.out.println("+2 months: " + jan31.plusMonths(2)); // Mar 317980// Practical examples81System.out.println("\nPractical examples:");8283// Quarterly dates84LocalDate q1End→ 2025-03-31 = LocalDate.of(2025, 3, 31);85LocalDate q2End→ 2025-06-30 = q1End.plusMonths(3);86LocalDate q3End→ 2025-09-30 = q2End.plusMonths(3);87LocalDate q4End→ 2025-12-30 = q3End.plusMonths(3);88System.out.println("Q1 end: " + q1End2025-03-31);89System.out.println("Q2 end: " + q2End2025-06-30);90System.out.println("Q3 end: " + q3End2025-09-30);91System.out.println("Q4 end: " + q4End2025-12-30);9293// Monthly billing dates94System.out.println("\nMonthly billing dates (starting " + date2024-02-29 + "):");95for (int i = 0; i < 6; i++) {
    output
    Add months (handle overflow):
    Jan 31: 2025-01-31
    +1 month: 2025-02-28
    +2 months: 2025-03-31
    
    Practical examples:
    Q1 end: 2025-03-31
    Q2 end: 2025-06-30
    Q3 end: 2025-09-30
    Q4 end: 2025-12-30
    
    Monthly billing dates (starting 2024-02-29):
  8. billing ← 2024-02-29

    pass 1 of 6
    94System.out.println("\nMonthly billing dates (starting " + date + "):");95for (int i0 = 0; i < 6; i++) {96    LocalDate billing→ 2024-02-29 = date.plusMonths(i0);97    System.out.println("Month " + i0 + ": " + billing2024-02-29);98}
    outputMonth 0: 2024-02-29
    All 6 passes — pass 1 is the card above
    passibilling
    102024-02-29
    212024-03-29
    322024-04-29
    432024-05-29
    542024-06-29
    652024-07-29
  9. System.out.println(" End of each month (2025):");

    100// End of each month101System.out.println("\nEnd of each month (2025):");102for (int m = 1; m <= 12; m++) {
    output
    End of each month (2025):
  10. monthEnd ← 2025-01-31

    pass 1 of 12
    101System.out.println("\nEnd of each month (2025):");102for (int m1 = 1; m <= 12; m++) {103    LocalDate monthEnd→ 2025-01-31 = YearMonth.of(2025, m1).atEndOfMonth();104    System.out.println(monthEnd.getMonth() + ": " + monthEnd2025-01-31);105}
    outputJANUARY: 2025-01-31
    All 12 passes — pass 1 is the card above
    passmmonthEnd
    112025-01-31
    222025-02-28
    332025-03-31
    442025-04-30
    552025-05-31
    662025-06-30
    772025-07-31
    882025-08-31
    992025-09-30
    10102025-10-31
    11112025-11-30
    12122025-12-31
TemporalAdjuster A strategy for adjusting dates, like finding the first/last day of month or next specific weekday.

Period vs Duration

  • Period: Date-based (days, months, years)
  • Duration: Time-based (hours, minutes, seconds)

Common Operations

  • Add/subtract days, months, years
  • Calculate duration between dates
  • Find day of week, month
  • First/last day of month

Exercise: Practical.java

Calculate the number of business days (excluding weekends) between two dates