Encodera

Thanks for reading!

Donate to the site — we work hard for you.

How to get the day of the week

Short answer

getDayOfWeek() returns a DayOfWeek enum value:

void main() {
    var date = LocalDate.of(2026, 3, 8);
    IO.println(date.getDayOfWeek());
}

Output:

SUNDAY

The name in a language

getDisplayName() returns the full or short name:

void main() {
    DayOfWeek day = LocalDate.of(2026, 3, 8).getDayOfWeek();
 
    IO.println(day.getDisplayName(TextStyle.FULL, Locale.ENGLISH));
    IO.println(day.getDisplayName(TextStyle.SHORT, Locale.GERMAN));
    IO.println(day.getDisplayName(TextStyle.FULL, Locale.of("ru")));
}

Output:

Sunday
So.
воскресенье

The day number

getValue() numbers the days from Monday (1) to Sunday (7):

void main() {
    IO.println(LocalDate.of(2026, 3, 9).getDayOfWeek().getValue());
    IO.println(LocalDate.of(2026, 3, 8).getDayOfWeek().getValue());
}

Output:

1
7

Is it a weekend

Compare with SATURDAY and SUNDAY:

boolean isWeekend(LocalDate date) {
    return switch (date.getDayOfWeek()) {
        case SATURDAY, SUNDAY -> true;
        default -> false;
    };
}
 
void main() {
    IO.println(isWeekend(LocalDate.of(2026, 3, 7)));
    IO.println(isWeekend(LocalDate.of(2026, 3, 9)));
}

Output:

true
false

The next Monday

TemporalAdjusters finds the nearest day of the week you need:

void main() {
    var date = LocalDate.of(2026, 3, 8);
    IO.println(date.with(TemporalAdjusters.next(DayOfWeek.MONDAY)));
}

Output:

2026-03-09

Common mistake

Assuming the week starts on Sunday, as in the old Calendar, where Sunday is day number 1. In java.time Monday is 1 and Sunday is 7.

Functions used in this recipe

Thanks for reading!

Donate to the site — we work hard for you.