Sub topic 6
DayOfWeek, Month, YearMonth, and MonthDay
Sometimes you need only part of a date. A shop may open every Friday. A bill may be due in March 2025. A birthday falls on the same day every year. java.time has small types for each of these cases, so you do not need a full LocalDate for them.
The four types
| Type | What it holds | Example value |
|---|---|---|
| `DayOfWeek` | A day of the week | `FRIDAY` |
| `Month` | A month of the year | `FEBRUARY` |
| `YearMonth` | A year and a month, with no day | `2024-02` |
| `MonthDay` | A month and a day, with no year | `--02-29` |
DayOfWeek and Month are enums. An enum is a type with a fixed list of named values. You can't make a new day of the week, so the seven days are already there for you. YearMonth and MonthDay are immutable value types, like LocalDate. Methods that change them return a new object.
A worked example
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.MonthDay;
import java.time.YearMonth;
public class DateParts {
public static void main(String[] args) {
DayOfWeek day = DayOfWeek.FRIDAY;
System.out.println(day.getValue()); // 5
System.out.println(day.plus(3)); // MONDAY
Month month = Month.FEBRUARY;
System.out.println(month.getValue()); // 2
System.out.println(month.length(true)); // 29
System.out.println(month.plus(11)); // JANUARY
YearMonth bill = YearMonth.of(2024, Month.FEBRUARY);
System.out.println(bill); // 2024-02
System.out.println(bill.lengthOfMonth()); // 29
System.out.println(bill.atEndOfMonth()); // 2024-02-29
MonthDay birthday = MonthDay.of(Month.FEBRUARY, 29);
System.out.println(birthday); // --02-29
System.out.println(birthday.isValidYear(2023)); // false
System.out.println(birthday.atYear(2023)); // 2023-02-28
LocalDate date = LocalDate.of(2024, 11, 5);
System.out.println(date.getDayOfWeek()); // TUESDAY
System.out.println(MonthDay.from(date)); // --11-05
}
}What the code shows
getValue()gives the number of a day or month. Monday is 1 and Sunday is 7. January is 1 and December is 12. Unlike the oldCalendarclass, months do not start at 0.pluswraps around. Three days after Friday is Monday, and eleven months after February is January.Month.length(boolean)needs to know whether the year is a leap year, becauseMonthhas no year. A leap year is a year with a February 29.YearMonthknows its year, solengthOfMonth()gives the exact answer.atEndOfMonth()turns it into a fullLocalDate.MonthDayhas no year, so February 29 is allowed.atYearbuilds a full date. If the year has no February 29, you get February 28 instead.
Common mistakes
- Comparing a day with a number. Write
day == DayOfWeek.SATURDAY, notday.getValue() == 6. The enum is clearer and harder to get wrong. - Printing
MonthDayand expecting a date.--02-29is the standard text form for "a month and day with no year".
Exercise
What does DayOfWeek.SUNDAY.plus(1) return, and what does YearMonth.of(2023, 2).lengthOfMonth() return?
Show answer
DayOfWeek.SUNDAY.plus(1) returns MONDAY, because the days wrap around after Sunday. YearMonth.of(2023, 2).lengthOfMonth() returns 28, because 2023 is not a leap year.
Exercise
A user was born on February 29. Write one line that gives their birthday as a LocalDate in 2025.
Show answer
MonthDay.of(Month.FEBRUARY, 29).atYear(2025) returns 2025-02-28, because 2025 has no February 29.