MonthDay Recurring Dates Leap Aware

MonthDay.of(2, 29) maps to Feb 29 on leap years and Feb 28 otherwise, so recurring annual dates survive the calendar edge case.

JavaForm Validation

Code

snippet
import java.time.LocalDate;
import java.time.MonthDay;
import java.time.Year;

public class MonthDayRecurringLeap {

    public static void main(String[] args) {
        MonthDay anniversary = MonthDay.of(2, 29); // 2 月 29 日

        // atYear:闰年得到 02-29,平年由 atYear 自动调整到 02-28
        System.out.println("in 2024 = " + anniversary.atYear(2024));
        System.out.println("in 2025 = " + anniversary.atYear(2025));

        // 平年策略自己定:isLeap 判断后是否顺延一天
        Year year = Year.of(2025);
        LocalDate celebration = year.isLeap()
                ? anniversary.atYear(year.getValue())
                : anniversary.atYear(year.getValue()).plusDays(1);
        System.out.println("celebration in 2025 = " + celebration);

        // 只比较月与日:跨年复用同一个 MonthDay
        MonthDay deadline = MonthDay.of(3, 1);
        System.out.println("anniversary before deadline = " + anniversary.isBefore(deadline));
        System.out.println("month-day of 2025-02-28 = " + MonthDay.from(LocalDate.of(2025, 2, 28)));
    }
}

How it works

Structurally, expect the class MonthDayRecurringLeap and the function main and nothing else.

What to watch for

  • The toolchain suffices on its own — no dependency resolution runs.

Using it

Entry signature: public static void main(String[] args)

The demo output is labeled (in 2024 =, in 2025 =, celebration in 2025 =), so the output can be read as a step-by-step trace.

Key statements

MonthDay anniversary = MonthDay.of(2, 29); // 2 月 29 日
// atYear:闰年得到 02-29,平年由 atYear 自动调整到 02-28
System.out.println("in 2024 = " + anniversary.atYear(2024));
: anniversary.atYear(year.getValue()).plusDays(1);
MonthDay deadline = MonthDay.of(3, 1);

Interactive Tools for This Topic

FAQ

What does MonthDay Recurring Dates Leap Aware print when run?
Running the file directly is enough — expect labeled output lines such as `in 2024 =`, `in 2025 =`, `celebration in 2025 =`.