Java Reference ยท Date and Time
DayOfWeek and Month enums
Reference for java.time DayOfWeek and Month: numeric values, plus() and minus() arithmetic, localized display names, lengths of months and common mistakes.
DayOfWeek and Month are enums in java.time with one constant per weekday (MONDAY to SUNDAY) and per month (JANUARY to DECEMBER). Using them instead of bare numbers makes date code self-documenting and removes the off-by-one mistakes that plagued the old Calendar class, where January was 0.
Both enums have a numeric value starting at 1 (Monday is 1 and Sunday is 7, in the ISO-8601 convention; January is 1), wrap-around arithmetic with plus and minus, and localized names through getDisplayName.
DayOfWeek d = LocalDate.of(2026, 3, 9).getDayOfWeek(); // MONDAY Month m = Month.of(12); // DECEMBER
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
public enum DayOfWeek implements TemporalAccessor, TemporalAdjuster public enum Month implements TemporalAccessor, TemporalAdjuster
| Member | Description |
|---|---|
of(int) |
The constant for 1-7 (DayOfWeek) or 1-12 (Month). Throws DateTimeException outside the range. |
from(TemporalAccessor) |
Extracts the value from a date, for example Month.from(date). |
getValue() |
The 1-based number. |
plus(long), minus(long) |
Wrap-around arithmetic; the argument may be negative or larger than the cycle. |
getDisplayName(TextStyle, Locale) |
Localized text; FULL, SHORT, NARROW and standalone variants. |
Month.length(boolean leapYear) |
Days in the month for a leap or non-leap year. |
Month.minLength(), maxLength() |
Smallest and largest possible number of days. |
Month.firstMonthOfQuarter() |
JANUARY, APRIL, JULY or OCTOBER. |
Month.firstDayOfYear(boolean leapYear) |
Day-of-year number of the first day. |
Usage notes
- Numbering. Do not use
ordinal(): forDayOfWeekthe ordinal is the value minus one, and it would silently break if someone assumed a Sunday start. CallgetValue(). - Week start is a locale matter. ISO says Monday is the first day, but the United States treats Sunday as the first. For locale-aware weeks use
WeekFields.of(locale). - Names depend on the locale. Always pass an explicit
Localein code with reproducible output; the default locale changes the result. - Both are adjusters.
date.with(DayOfWeek.FRIDAY)returns the Friday of the same ISO week. For "next Friday" useTemporalAdjusters. - Switch and EnumSet fit naturally. They are enums, so
switchexpressions can be exhaustive andEnumSetis the compact way to store a set of days. - No year information.
Month.FEBRUARY.length(true)needs you to say whether it is a leap year; askYearwithisLeap()orYearMonth.lengthOfMonth().
Related
LocalDate,TemporalAdjusters,YearMonth,ChronoUnit- Lessons: Dates and times