LocalDate, Period and Temporal Adjusters in Java
Work with dates without times or zones: creating and validating LocalDate values, month-end clamping in arithmetic, Period versus days, ages and differences, DayOfWeek and Month, TemporalAdjusters and custom adjusters, date streams, YearMonth, MonthDay, ISO weeks and other calendars.
kvi5hnuCode Sprout team member10 min readOn this page
Many dates aren't points in time at all. A birthday, a public holiday, an invoice date or "the first Monday of next month" is a calendar date that people everywhere read the same way, whatever their time zone. Java models these with local date and time classes, which have no time zone. This article covers LocalDate and its helpers. Core Java I's article 29 introduced the basics, and here we go further.
Creating dates
A LocalDate holds a year, a month (1 to 12, at last!) and a day:
LocalDate.now()(ornow(clock),now(zone)) gives today's date. Note that "today" depends on the time zone, even though the result has none.LocalDate.of(1903, 6, 14)orLocalDate.of(1903, Month.JUNE, 14)builds a date. Invalid values such as February 29 in a non-leap year throw aDateTimeException.LocalDate.parse("2026-11-14")reads ISO format (other formats are covered in article 70).ofYearDay(year, day),ofEpochDay(n)(days since 1970-01-01, the inverse oftoEpochDay()), andofInstant(instant, zone)(Java 9) cover other sources.
Getters return the parts: getYear, getMonth (an enum), getMonthValue, getDayOfMonth, getDayOfYear, getDayOfWeek, lengthOfMonth, lengthOfYear, isLeapYear. Compare dates with isBefore, isAfter, isEqual and compareTo.
The leap-year rule (divisible by 4, except centuries, except every 400 years) is applied to all years, including those before the Gregorian calendar existed. That's the proleptic Gregorian calendar, which ISO-8601 uses.
Date arithmetic
plusDays, plusWeeks, plusMonths, plusYears and their minus counterparts return new dates. The with methods replace one field: withDayOfMonth, withMonth, withYear, withDayOfYear, or generally with(ChronoField.X, value).
Adding months or years can produce a day that doesn't exist, such as January 31 plus one month. Instead of failing, LocalDate clamps to the last valid day: February 28 (or 29). This has consequences:
- Adding one month twice isn't the same as adding two months. January 31 plus 1 month plus 1 month is March 28, while January 31 plus 2 months is March 31.
- February 29, 2024 plus one year is February 28, 2025.
For repeating schedules, such as a monthly bill on the 31st, always compute from the original date (start.plusMonths(n)), not from the previous result.
Period: amounts of calendar time
Period is the calendar counterpart of Duration: a number of years, months and days, which have no fixed length. Period.of(1, 2, 3), ofDays, ofWeeks (stored as days), ofMonths, ofYears and parse("P1Y2M3D") create periods. getYears(), getMonths() and getDays() read them, and toTotalMonths() and normalized() (which turns 14 months into a year and 2 months, but leaves days alone) convert them.
date.plus(Period.ofYears(1))respects the calendar, whiledate.plusDays(365)lands on the wrong day after a leap day.- When a period with months and days is added, the months are added first, then the days.
Period.between(start, end)orstart.until(end)gives the difference as years, months and days. This is the right way to compute a person's age.Period.between(Jan 31, Mar 1)isP1M1D.- For a plain count of days (or weeks), use
ChronoUnit.DAYS.between(start, end)orstart.until(end, ChronoUnit.DAYS). APeriodof "5 months and 21 days" can't be turned into days without knowing which months.
Days of the week and months
DayOfWeek and Month are enums. Following ISO-8601, Monday is 1 and Sunday is 7 (unlike Calendar, where Sunday was 1). Both have plus and minus that wrap around: DayOfWeek.SATURDAY.plus(3) is TUESDAY, and Month.JANUARY.minus(1) is DECEMBER. getDisplayName(TextStyle.FULL, locale) gives localized names (article 70).
Which day starts the week, and what counts as week 1 of the year, differ between countries. WeekFields.of(locale) gives the local rules, and IsoFields.WEEK_OF_WEEK_BASED_YEAR the ISO rule used in Europe. Under the ISO rule, a week belongs to the year that contains its Thursday. So January 1, 2027, a Friday, is in week 53 of the week-based year 2026. IsoFields.QUARTER_OF_YEAR gives the quarter.
Temporal adjusters
Scheduling rules like "the last Friday of the month" are expressed as a TemporalAdjuster, passed to with. The TemporalAdjusters class provides the common ones:
| Adjuster | Result |
|---|---|
firstDayOfMonth(), lastDayOfMonth() | first or last day of the date's month |
firstDayOfNextMonth(), firstDayOfYear(), lastDayOfYear(), firstDayOfNextYear() | other boundaries |
next(dow), previous(dow) | the next or previous given weekday, strictly after or before |
nextOrSame(dow), previousOrSame(dow) | the same, but the date itself counts if it matches |
firstInMonth(dow), lastInMonth(dow) | the first or last given weekday in the month |
dayOfWeekInMonth(n, dow) | the n-th given weekday in the month (negative n counts from the end) |
TemporalAdjuster is a functional interface that works on the general Temporal type. For date logic, TemporalAdjusters.ofDateAdjuster(UnaryOperator<LocalDate>) lets you write your own adjuster in terms of LocalDate, such as "next working day".
Streams of dates
datesUntil(endExclusive) (Java 9) streams every date up to an end, and datesUntil(end, step) steps by a period. Counting weekdays in a month, or listing the first Monday of each month, takes one stream pipeline.
Partial dates
Sometimes you only have part of a date:
MonthDayis a recurring date such as a birthday or December 25.isValidYear(year)checks whether, for example, February 29 exists in that year, andatYear(year)turns it into a date (clamping February 29 to the 28th).YearMonthis a month, such as a credit card expiry or a billing period.atDay(n),atEndOfMonth()andlengthOfMonth()help with it.Yearis a year, withisLeap(year)andatDay(dayOfYear).
Other calendars
LocalDate uses the ISO calendar. The package java.time.chrono also supports the Thai Buddhist, Japanese imperial, Minguo (Taiwan) and Hijrah (Islamic Umm al-Qura) calendars through ThaiBuddhistDate, JapaneseDate, MinguoDate and HijrahDate, all implementing ChronoLocalDate. Convert with XxxDate.from(localDate) and back with LocalDate.from(chronoDate). Use them for display. Keep application logic and storage in ISO LocalDate.
The program
The demo creates dates in several ways and rejects February 29, 2026. It shows month-end clamping and why adding one month twice differs from adding two. It computes the time until Christmas as a period, in days and in weeks, an age, and the normalized form of a period. After weekday arithmetic, it applies the standard adjusters to Saturday, November 14, 2026, including US Thanksgiving, and a custom next-workday adjuster. It uses date streams to find the first Mondays of a quarter and to count weekdays. Finally it handles a February 29 birthday, a card expiry month, ISO and US week numbers, and the same date in four other calendars.
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.*;
import java.util.*;
import java.util.stream.*;
public class Solution {
// A custom adjuster: the next working day (Monday to Friday)
static final TemporalAdjuster NEXT_WORKDAY = TemporalAdjusters.ofDateAdjuster(date -> {
LocalDate d = date;
do { d = d.plusDays(1); } while (d.getDayOfWeek().getValue() >= 6);
return d;
});
static String age(LocalDate born, LocalDate today) {
Period p = Period.between(born, today);
return p.getYears() + " years, " + p.getMonths() + " months, " + p.getDays() + " days";
}
public static void main(String[] args) {
System.out.println("--- creating local dates ---");
LocalDate church = LocalDate.of(1903, 6, 14);
LocalDate same = LocalDate.of(1903, Month.JUNE, 14);
System.out.println(church + " equals " + same + ": " + church.equals(same) + ", " + church.getDayOfWeek()
+ ", day " + church.getDayOfYear() + " of the year");
System.out.println("parsed: " + LocalDate.parse("2026-11-14") + ", from epoch day: " + LocalDate.ofEpochDay(20_000)
+ ", from day of year: " + LocalDate.ofYearDay(2026, 256));
System.out.println("from an instant in Tokyo: " + LocalDate.ofInstant(Instant.parse("2026-11-14T20:00:00Z"), ZoneId.of("Asia/Tokyo")));
try {
LocalDate.of(2026, 2, 29);
} catch (DateTimeException e) {
System.out.println("invalid: " + e.getMessage());
}
System.out.println("--- arithmetic ---");
LocalDate d = LocalDate.of(2026, 1, 31);
System.out.println(d + " plus 1 month = " + d.plusMonths(1) + ", plus 2 months = " + d.plusMonths(2)
+ ", plus 1 month twice = " + d.plusMonths(1).plusMonths(1));
System.out.println("2024-02-29 plus 1 year = " + LocalDate.of(2024, 2, 29).plusYears(1)
+ ", plus 365 days = " + LocalDate.of(2024, 2, 29).plusDays(365));
System.out.println("with: " + d.withDayOfMonth(15) + ", " + d.withMonth(6) + ", " + d.with(ChronoField.DAY_OF_YEAR, 100));
LocalDate july4 = LocalDate.of(2026, 7, 4), christmas = LocalDate.of(2026, 12, 25);
System.out.println("until christmas: " + july4.until(christmas) + " = " + july4.until(christmas, ChronoUnit.DAYS) + " days = "
+ ChronoUnit.WEEKS.between(july4, christmas) + " whole weeks");
System.out.println("leap years: 2024 " + Year.isLeap(2024) + ", 1900 " + Year.isLeap(1900) + ", 2000 " + Year.isLeap(2000)
+ "; length of Feb 2028: " + YearMonth.of(2028, 2).lengthOfMonth());
System.out.println("--- periods ---");
Period p = Period.of(1, 14, 40);
System.out.println(p + " normalized " + p.normalized() + ", total months " + p.toTotalMonths() + ", parse "
+ Period.parse("P2Y3M") + ", weeks " + Period.ofWeeks(3));
System.out.println("age: " + age(LocalDate.of(1990, 11, 20), LocalDate.of(2026, 11, 14)));
System.out.println("adding a period to a date: " + LocalDate.of(2026, 1, 31).plus(Period.of(0, 1, 1)));
System.out.println("Period.between(Jan 31, Mar 1): " + Period.between(LocalDate.of(2026, 1, 31), LocalDate.of(2026, 3, 1)));
System.out.println("--- days of the week ---");
System.out.println("MONDAY = " + DayOfWeek.MONDAY.getValue() + ", SUNDAY = " + DayOfWeek.SUNDAY.getValue()
+ ", SATURDAY.plus(3) = " + DayOfWeek.SATURDAY.plus(3) + ", Month.JANUARY.minus(1) = " + Month.JANUARY.minus(1));
System.out.println("--- adjusters ---");
LocalDate today = LocalDate.of(2026, 11, 14); // a Saturday
System.out.println("today " + today + " " + today.getDayOfWeek());
System.out.println("first day of month: " + today.with(TemporalAdjusters.firstDayOfMonth())
+ ", last: " + today.with(TemporalAdjusters.lastDayOfMonth())
+ ", first of next month: " + today.with(TemporalAdjusters.firstDayOfNextMonth()));
System.out.println("next Tuesday: " + today.with(TemporalAdjusters.next(DayOfWeek.TUESDAY))
+ ", next or same Saturday: " + today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY))
+ ", previous Friday: " + today.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY)));
System.out.println("first Monday: " + today.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY))
+ ", last Friday: " + today.with(TemporalAdjusters.lastInMonth(DayOfWeek.FRIDAY))
+ ", 4th Thursday (US Thanksgiving): " + today.with(TemporalAdjusters.dayOfWeekInMonth(4, DayOfWeek.THURSDAY)));
System.out.println("next workday after " + today + ": " + today.with(NEXT_WORKDAY)
+ ", after 2026-11-11: " + LocalDate.of(2026, 11, 11).with(NEXT_WORKDAY));
System.out.println("--- streams of dates ---");
System.out.println("first Mondays of 2027 Q1: " + LocalDate.of(2027, 1, 1).datesUntil(LocalDate.of(2027, 4, 1), Period.ofMonths(1))
.map(m -> m.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY))).toList());
long workdays = LocalDate.of(2026, 12, 1).datesUntil(LocalDate.of(2027, 1, 1))
.filter(x -> x.getDayOfWeek().getValue() < 6).count();
System.out.println("weekdays in December 2026: " + workdays);
System.out.println("--- partial dates and weeks ---");
MonthDay birthday = MonthDay.of(Month.FEBRUARY, 29);
System.out.println(birthday + " valid in 2026: " + birthday.isValidYear(2026) + ", at 2026: " + birthday.atYear(2026));
YearMonth card = YearMonth.of(2028, 2);
System.out.println("card expires " + card + ", last day " + card.atEndOfMonth() + ", expired on 2028-03-01: " + LocalDate.of(2028, 3, 1).isAfter(card.atEndOfMonth()));
LocalDate newYear = LocalDate.of(2027, 1, 1);
System.out.println(newYear + " is in ISO week " + newYear.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR) + " of week-based year "
+ newYear.get(IsoFields.WEEK_BASED_YEAR) + ", quarter " + newYear.get(IsoFields.QUARTER_OF_YEAR)
+ "; US week of year: " + newYear.get(WeekFields.of(Locale.US).weekOfYear()));
System.out.println("--- other calendars ---");
System.out.println(ThaiBuddhistDate.from(today) + " | " + JapaneseDate.from(today) + " | " + HijrahDate.from(today) + " | " + MinguoDate.from(today));
}
}
--- creating local dates ---
1903-06-14 equals 1903-06-14: true, SUNDAY, day 165 of the year
parsed: 2026-11-14, from epoch day: 2024-10-04, from day of year: 2026-09-13
from an instant in Tokyo: 2026-11-15
invalid: Invalid date 'February 29' as '2026' is not a leap year
--- arithmetic ---
2026-01-31 plus 1 month = 2026-02-28, plus 2 months = 2026-03-31, plus 1 month twice = 2026-03-28
2024-02-29 plus 1 year = 2025-02-28, plus 365 days = 2025-02-28
with: 2026-01-15, 2026-06-30, 2026-04-10
until christmas: P5M21D = 174 days = 24 whole weeks
leap years: 2024 true, 1900 false, 2000 true; length of Feb 2028: 29
--- periods ---
P1Y14M40D normalized P2Y2M40D, total months 26, parse P2Y3M, weeks P21D
age: 35 years, 11 months, 25 days
adding a period to a date: 2026-03-01
Period.between(Jan 31, Mar 1): P1M1D
--- days of the week ---
MONDAY = 1, SUNDAY = 7, SATURDAY.plus(3) = TUESDAY, Month.JANUARY.minus(1) = DECEMBER
--- adjusters ---
today 2026-11-14 SATURDAY
first day of month: 2026-11-01, last: 2026-11-30, first of next month: 2026-12-01
next Tuesday: 2026-11-17, next or same Saturday: 2026-11-14, previous Friday: 2026-11-13
first Monday: 2026-11-02, last Friday: 2026-11-27, 4th Thursday (US Thanksgiving): 2026-11-26
next workday after 2026-11-14: 2026-11-16, after 2026-11-11: 2026-11-12
--- streams of dates ---
first Mondays of 2027 Q1: [2027-01-04, 2027-02-01, 2027-03-01]
weekdays in December 2026: 23
--- partial dates and weeks ---
--02-29 valid in 2026: false, at 2026: 2026-02-28
card expires 2028-02, last day 2028-02-29, expired on 2028-03-01: true
2027-01-01 is in ISO week 53 of week-based year 2026, quarter 1; US week of year: 1
--- other calendars ---
ThaiBuddhist BE 2569-11-14 | Japanese Reiwa 8-11-14 | Hijrah-umalqura AH 1448-06-04 | Minguo ROC 115-11-14
Common questions
How do I calculate someone's age?
Period.between(birthDate, LocalDate.now(clock)).getYears(), or ChronoUnit.YEARS.between(birthDate, today). Both count completed years. Decide explicitly how a February 29 birthday is treated in non-leap years; between treats February 28 as not yet the birthday.
How many days are there between two dates?
ChronoUnit.DAYS.between(start, end), which counts the end exclusively. Period.between(start, end).getDays() is a common mistake: it returns only the day component (for example 3 in "1 month and 3 days").
Why does LocalDate.parse("2026-02-29") throw, but a custom formatter accepts it?
The ISO formatter uses the strict resolver. A formatter from ofPattern uses the default SMART resolver, which turns February 29 into February 28 in a non-leap year without complaint (verified on JDK 25). Use withResolverStyle(ResolverStyle.STRICT) with uuuu for validation (article 70).
How do I find all working days in a period, excluding holidays?
start.datesUntil(end).filter(d -> d.getDayOfWeek().getValue() < 6).filter(d -> !holidays.contains(d)). The JDK has no holiday calendar; holidays vary by country and region and come from your own data or a library.
Does LocalDate work for historical dates before 1582?
It computes them in the proleptic Gregorian calendar. Historical documents from that time use the Julian calendar, which differs from the proleptic Gregorian one by a number of days that depends on the century (10 days in 1582, fewer in earlier centuries). Countries also switched at different dates (Britain in 1752, Russia in 1918). For accurate historical dates, you need a Julian chronology from a library such as ThreeTen-Extra.
API reference
| API | Purpose |
|---|---|
LocalDate.now, of, parse, ofYearDay, ofEpochDay, ofInstant | create dates |
getYear, getMonth, getMonthValue, getDayOfMonth, getDayOfYear, getDayOfWeek, lengthOfMonth, isLeapYear | read parts |
plusDays, plusWeeks, plusMonths, plusYears, minusXxx, plus(Period) | arithmetic (clamps to month end) |
withDayOfMonth, withMonth, withYear, with(field, value), with(adjuster) | replace fields |
isBefore, isAfter, isEqual, until(end), until(end, unit), datesUntil | compare, differences, streams |
Period.of, ofDays, ofWeeks, ofMonths, ofYears, between, parse, normalized, toTotalMonths | calendar amounts |
ChronoUnit.DAYS.between(a, b) | count days or other units |
DayOfWeek, Month with plus, minus, getValue, getDisplayName | weekday and month enums |
TemporalAdjusters.firstDayOfMonth, lastDayOfMonth, next, nextOrSame, firstInMonth, lastInMonth, dayOfWeekInMonth, ofDateAdjuster | adjusters |
MonthDay, YearMonth, Year | partial dates |
WeekFields.of(locale), IsoFields.WEEK_OF_WEEK_BASED_YEAR, QUARTER_OF_YEAR | weeks and quarters |
ThaiBuddhistDate, JapaneseDate, MinguoDate, HijrahDate | other calendars |
Summary
LocalDateis a calendar date without a time zone. Use local types for birthdays, holidays and schedules, and zoned types only for actual moments.- Month and year arithmetic clamps to the last valid day of the month, so compute repeating dates from the original date.
Periodmeasures years, months and days and is right for ages and calendar intervals. UseChronoUnit.DAYS.betweenfor day counts.DayOfWeekandMonthare enums with wrap-around arithmetic. Week numbering depends on the locale, so useIsoFieldsorWeekFieldsexplicitly.TemporalAdjustersanswer questions like "last Friday of the month",datesUntilstreams dates, andYearMonth,MonthDayandYearmodel partial dates.
Practice Questions
A subscription renews on the 31st. Why is next = previous.plusMonths(1) wrong, and what should you do?
After January 31, the next date is February 28, and every later renewal stays on the 28th, because each step starts from the clamped date. Compute each renewal from the original start date: start.plusMonths(n) gives March 31, April 30, May 31, and so on.
How do you compute someone's age in whole years on a given date?
Period.between(birthDate, today).getYears(), or ChronoUnit.YEARS.between(birthDate, today). Both count completed years. Don't divide a day count by 365, which goes wrong around birthdays because of leap years.
How do you find the last business day (Monday to Friday) of a month?
Start from the last day of the month, date.with(TemporalAdjusters.lastDayOfMonth()), and step back while it's a Saturday or Sunday. A custom adjuster with TemporalAdjusters.ofDateAdjuster makes this reusable. Real business calendars also need a list of public holidays.
Why can LocalDate.now() return different dates on two servers at the same moment?
"Today" depends on the time zone. now() uses the system default zone, so a server in Tokyo and one in New York can be on different dates at the same instant. Pass an explicit zone, LocalDate.now(ZoneId.of("Europe/Berlin")), or a Clock configured with the business's zone.
In which week of which year is Friday, January 1, 2027, under ISO rules, and why?
ISO week 53 of the week-based year 2026. ISO weeks start on Monday, and a week belongs to the year that contains its Thursday. The week from Monday, December 28, 2026 to Sunday, January 3, 2027 has its Thursday on December 31, 2026, so it's the last week of 2026. Use IsoFields.WEEK_BASED_YEAR together with the week number, not the calendar year.
Previous: Instant and Duration in Java: Points and Spans on the Time Line
Next: LocalTime, ZonedDateTime and Daylight Saving Time in Java
Related reading
- Instant and Duration in Java: Points and Spans on the Time Line
- Connection Pools, DataSources and JPA: JDBC in Real Applications
- JDBC Transactions: Commit, Rollback, Isolation, Save Points and Batch Updates
- JDBC Metadata: DatabaseMetaData, ResultSetMetaData and ParameterMetaData
- JDBC Row Sets: CachedRowSet, WebRowSet, FilteredRowSet and JoinRowSet

More in Core Java II
FFM Upcalls, Variadic Functions and errno
Advanced FFM calls: passing Java code to C as function pointers with upcall stubs, binding objects and lambdas, calling variadic functions such as printf, capturing errno and GetLastError, and a C library that calls back into Java. The final article of the course.
Calling Foreign Functions: Linker, Symbol Lookup, Downcalls and jextract
Calling C functions from pure Java with the FFM API: the native linker, default, library and loader symbol lookups, function descriptors, downcall handles, invokeExact, pointers and NULL, struct results, critical calls, enabling native access, and generating bindings with jextract.
FFM Memory Layouts: Structs, Unions, Sequences and Var Handles
Describing C data with FFM memory layouts: value layouts and C types, struct and union layouts, alignment and padding, sequences, layout paths, var handles with base offsets, offset and slice handles, pointers with target layouts, and atomic access modes.
Foreign Function and Memory API: Arenas and Memory Segments
Getting started with the FFM API (final since Java 22): a first foreign call, confined, shared, automatic and global arenas, native and heap memory segments, reading and writing values, C strings and arrays, alignment and byte order, slices, copies, allocators, and memory-mapped files.
Discussion
Sign in to ask a question or share what you learned.