Working with Enums: DayOfWeek and Month in Java (java.time API)

Illustration for Working with Enums: DayOfWeek and Month in Java (java.time API)
By Last updated:

Enums like DayOfWeek and Month are the backbone of calendar-based logic in Java.

  • A scheduling app checks if today is Monday to trigger weekly reports.
  • A banking system calculates interest every quarter (March, June, September, December).
  • A logging framework labels events with day and month values.

A common mistake developers face is reinventing day/month constants manually, instead of leveraging the powerful, type-safe enums available in java.time.


1. DayOfWeek Basics

LocalDate today = LocalDate.now();
DayOfWeek day = today.getDayOfWeek();
System.out.println("Today is: " + day);

✅ Prints the enum value (THURSDAY).
✅ Provides type safety compared to integers.


2. Using DayOfWeek in Scheduling

LocalDate nextMonday = LocalDate.now()
    .with(TemporalAdjusters.next(DayOfWeek.MONDAY));

System.out.println("Next Monday: " + nextMonday);

✅ Great for recurring tasks like weekly meetings.


3. Month Basics

LocalDate date = LocalDate.of(2025, Month.AUGUST, 28);
System.out.println("Month: " + date.getMonth()); // AUGUST
System.out.println("Month Value: " + date.getMonthValue()); // 8

Month enums give both name and numeric values.


4. Iterating Over Months

for (Month month : Month.values()) {
    System.out.println(month + " - Days: " + month.length(false));
}

✅ Useful for financial quarters, billing cycles, and reports.


5. Combining Enums with Logic

DayOfWeek today = LocalDate.now().getDayOfWeek();

if (today == DayOfWeek.SATURDAY || today == DayOfWeek.SUNDAY) {
    System.out.println("It's the weekend!");
} else {
    System.out.println("It's a workday.");
}

✅ Clear and readable scheduling logic.


6. Common Pitfalls and Anti-Patterns

  • ❌ Using integers (1–7, 1–12) instead of enums → prone to off-by-one errors.
  • ❌ Forgetting leap years when calculating month lengths manually.
  • ❌ Hardcoding day names as strings.
  • ❌ Mixing legacy constants (Calendar.MONDAY) with DayOfWeek.

7. Best Practices

  • ✅ Use DayOfWeek and Month for clarity and safety.
  • ✅ Combine with TemporalAdjusters for advanced scheduling.
  • ✅ Prefer enums over magic numbers.
  • ✅ Use length(boolean leapYear) for correct month lengths.

📌 What's New in Java Versions?

  • Java 8: Introduced DayOfWeek and Month enums in java.time.
  • Java 11: Enhanced enum utility methods.
  • Java 17: Stable API, no major changes.
  • Java 21: Updated leap year logic and time zone data, no API change.

✅ API stable since Java 8.


Real-World Analogy

Think of DayOfWeek and Month like pre-labeled drawers in a cabinet:

  • You don’t need to guess which drawer is “third” or “fifth.”
  • The label itself (MONDAY, MARCH) makes your logic self-documenting.

Conclusion + Key Takeaways

  • ❌ Avoid integers or string-based day/month representations.
  • ✅ Use DayOfWeek and Month enums for clean, safe, and readable code.
  • ✅ Combine with adjusters and date-time classes for powerful scheduling.
  • ✅ Stable API ensures long-term reliability.

Enums prevent bugs, confusion, and hard-to-maintain code, making them essential in enterprise-grade scheduling and calendar systems.


FAQ: Expert-Level Q&A

1. Can DayOfWeek values be converted to integers?
Yes, getValue() returns 1 (Monday) to 7 (Sunday).

2. What is the value range of Month enums?
1 (January) to 12 (December).

3. How to check if a month has 31 days?
Use month.length(leapYear).

4. Can I parse a day or month from text?
Yes, with DayOfWeek.valueOf("MONDAY") or Month.valueOf("JANUARY").

5. What’s the difference between Calendar constants and DayOfWeek?
DayOfWeek is type-safe, modern, and aligned with ISO-8601.

6. Can enums be localized?
Yes, use getDisplayName(TextStyle, Locale).

7. Are DayOfWeek and Month comparable?
Yes—they implement Comparable.

8. Can I iterate over all days or months?
Yes, with .values().

9. How to find the quarter of a given month?
(month.getValue() - 1) / 3 + 1.

10. Are these enums immutable and thread-safe?
Yes, they are constants and safe for all contexts.