Java Reference ยท Date and Time
Year, YearMonth and MonthDay
Reference for java.time Year, YearMonth and MonthDay: model partial dates such as card expiry, billing months and birthdays, with leap-year handling.
Not every date has a day, a month and a year. A credit card expires in "03/2028", a report covers "October 2026", and a birthday repeats on "29 February" every year. Three small value classes in java.time model exactly these partial dates:
Yearis a calendar year (2026).YearMonthis a year and a month with no day.MonthDayis a month and a day with no year.
Using them instead of a LocalDate with a fake "1st of the month" removes a whole class of bugs. All three are immutable, thread-safe and comparable.
YearMonth card = YearMonth.of(2028, 3); boolean valid = !card.isBefore(YearMonth.of(2026, 10));
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
Year.of(int isoYear) Year.isLeap(long year) YearMonth.of(int year, int month) YearMonth.of(int year, Month month) MonthDay.of(int month, int dayOfMonth) MonthDay.of(Month month, int dayOfMonth)
| Class | Useful members |
|---|---|
Year |
of, now, isLeap(), length() (365 or 366), atDay(int), atMonth(...), plusYears, getValue(). |
YearMonth |
of, from(temporal), parse, lengthOfMonth(), lengthOfYear(), atDay(int), atEndOfMonth(), plusMonths, minusMonths, getMonth(), getYear(). |
MonthDay |
of, from(temporal), parse, atYear(int), isValidYear(int), getMonth(), getDayOfMonth(), isBefore, isAfter. |
Throws: DateTimeException for a month outside 1-12, a day outside the month (YearMonth.atDay(31) in February), or MonthDay.of(2, 30). Parsing failures throw DateTimeParseException.
Usage notes
- Text forms.
toString()gives ISO-8601:2028-02forYearMonth,--02-29forMonthDay,2028forYear. Parse the same shapes withparse(text), or supply a formatter for03/2028. - Feb 29 is allowed in
MonthDay.MonthDay.of(2, 29)is valid because the class has no year.atYear(2027)adjusts to 28 February in non-leap years. - Leap-year rule. A year is leap if divisible by 4, except century years not divisible by 400, so 1900 is not leap and 2000 is.
Year.isLeapuses this proleptic Gregorian rule even for dates before 1582. - Natural ordering. All three implement
Comparable, so they sort and work asTreeMapkeys.MonthDaycompares by month, then day. - Do not build them with the clock in tests.
Year.now()andYearMonth.now()read the system clock; pass aClockto make code testable. - Persistence. Store a
YearMonthas the text2028-03or two integers; do not store it as2028-03-01, which invites misuse as a day.