Period and Date Arithmetic
Add and subtract dates.
Period and Date Arithmetic is a free Java Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Date-Based Amounts
While Duration measures time in hours and seconds, Period measures amounts in years, months, and days. It is the right tool for calendar arithmetic like ages and deadlines.
Adding Days
LocalDate offers plusDays to move forward. Remember the result is a new immutable value.
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate start = LocalDate.of(2026, 5, 30);
System.out.println(start.plusDays(10));
}
}Adding Months and Years
plusMonths and plusYears shift by larger units and correctly handle month lengths.
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate start = LocalDate.of(2026, 1, 31);
System.out.println(start.plusMonths(1));
System.out.println(start.plusYears(2));
}
}Subtracting Dates
Use minusDays, minusWeeks, and similar methods to move backward in time.
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2026, 5, 30);
System.out.println(date.minusWeeks(2));
}
}Creating a Period
Build a Period with of or the single-unit factories like ofMonths, then add it to a date.
import java.time.LocalDate;
import java.time.Period;
public class Main {
public static void main(String[] args) {
Period p = Period.of(1, 2, 3);
LocalDate date = LocalDate.of(2026, 1, 1);
System.out.println(date.plus(p));
}
}Period Between Dates
Period.between measures the gap between two dates as years, months, and days.
import java.time.LocalDate;
import java.time.Period;
public class Main {
public static void main(String[] args) {
LocalDate from = LocalDate.of(2020, 1, 15);
LocalDate to = LocalDate.of(2026, 5, 30);
Period p = Period.between(from, to);
System.out.println(p.getYears() + "y " + p.getMonths() + "m " + p.getDays() + "d");
}
}Calculating an Age
A classic use of Period.between is computing someone's age in whole years.
import java.time.LocalDate;
import java.time.Period;
public class Main {
public static void main(String[] args) {
LocalDate birth = LocalDate.of(2000, 6, 15);
LocalDate today = LocalDate.of(2026, 5, 30);
int age = Period.between(birth, today).getYears();
System.out.println(age);
}
}Counting Total Days
Period splits the gap into years, months, and days. For a single total count of days, use ChronoUnit.DAYS.between instead.
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String[] args) {
LocalDate from = LocalDate.of(2026, 1, 1);
LocalDate to = LocalDate.of(2026, 12, 31);
long days = ChronoUnit.DAYS.between(from, to);
System.out.println(days);
}
}ChronoUnit Flexibility
ChronoUnit can count in many units: DAYS, WEEKS, MONTHS, and YEARS. Each between call returns a single number.
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String[] args) {
LocalDate from = LocalDate.of(2026, 1, 1);
LocalDate to = LocalDate.of(2026, 12, 31);
System.out.println(ChronoUnit.WEEKS.between(from, to));
System.out.println(ChronoUnit.MONTHS.between(from, to));
}
}Finding the Next Weekday
The TemporalAdjusters class provides smart adjustments, such as jumping to the first day of next month.
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2026, 5, 30);
System.out.println(date.with(TemporalAdjusters.firstDayOfNextMonth()));
}
}Period Versus ChronoUnit
Use Period when you want a human breakdown of years, months, and days. Use ChronoUnit.between when you want a single total count in one unit.
Quick Check
Test your date arithmetic skills.
Recap
You learned date arithmetic:
plusDays,plusMonths, andplusYearsshift dates immutablyPeriodmeasures years, months, and days, ideal for agesPeriod.betweengives a human breakdown of the gapChronoUnit.betweengives a single total in a chosen unitTemporalAdjustershandles smart jumps like first day of next month
Frequently asked questions
Is the “Period and Date Arithmetic” lesson free?
Yes — the full text of “Period and Date Arithmetic” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Period and Date Arithmetic”?
Add and subtract dates. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Period and Date Arithmetic” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- LocalDate, LocalTime, LocalDateTime
- Instant and Duration
- Period and Date Arithmetic
- Formatting with DateTimeFormatter