Java : DayOfWeek with Examples

DayOfWeek (Java SE 21 & JDK 21) with Examples.
You will find code examples on most DayOfWeek methods.


Summary

A day-of-week, such as 'Tuesday'.

Class diagram

final var date = LocalDate.of(2100, 12, 1);
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
System.out.println(date.format(formatter)); // Wednesday, December 1, 2100

final var ret = date.getDayOfWeek();
System.out.println(ret); // WEDNESDAY

Enum Constants

FRIDAY

The singleton instance for the day-of-week of Friday.

System.out.println(DayOfWeek.FRIDAY); // FRIDAY

MONDAY

The singleton instance for the day-of-week of Monday.

System.out.println(DayOfWeek.MONDAY); // MONDAY

SATURDAY

The singleton instance for the day-of-week of Saturday.

System.out.println(DayOfWeek.SATURDAY); // SATURDAY

SUNDAY

The singleton instance for the day-of-week of Sunday.

System.out.println(DayOfWeek.SUNDAY); // SUNDAY

THURSDAY

The singleton instance for the day-of-week of Thursday.

System.out.println(DayOfWeek.THURSDAY); // THURSDAY

TUESDAY

The singleton instance for the day-of-week of Tuesday.

System.out.println(DayOfWeek.TUESDAY); // TUESDAY

WEDNESDAY

The singleton instance for the day-of-week of Wednesday.

System.out.println(DayOfWeek.WEDNESDAY); // WEDNESDAY

Methods

Temporal adjustInto (Temporal temporal)

Adjusts the specified temporal object to have this day-of-week.

final var temporal = LocalDate.of(2100, 12, 17);
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
System.out.println(temporal.format(formatter)); // Friday, December 17, 2100

final var ret1 = DayOfWeek.THURSDAY.adjustInto(temporal);
System.out.println(ret1); // 2100-12-16

final var ret2 = DayOfWeek.FRIDAY.adjustInto(temporal);
System.out.println(ret2); // 2100-12-17

final var ret3 = DayOfWeek.SATURDAY.adjustInto(temporal);
System.out.println(ret3); // 2100-12-18

static DayOfWeek from (TemporalAccessor temporal)

Obtains an instance of DayOfWeek from a temporal object.

final var temporal = LocalDate.of(2100, 12, 17);
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
System.out.println(temporal.format(formatter)); // Friday, December 17, 2100

System.out.println(DayOfWeek.from(temporal)); // FRIDAY
final var temporal = LocalDate.of(2100, 12, 18);
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
System.out.println(temporal.format(formatter)); // Saturday, December 18, 2100

System.out.println(DayOfWeek.from(temporal)); // SATURDAY

int get (TemporalField field)

Gets the value of the specified field from this day-of-week as an int.

System.out.println(DayOfWeek.FRIDAY.isSupported(ChronoField.DAY_OF_WEEK)); // true
System.out.println(DayOfWeek.FRIDAY.get(ChronoField.DAY_OF_WEEK)); // 5

System.out.println(DayOfWeek.SUNDAY.isSupported(ChronoField.DAY_OF_WEEK)); // true
System.out.println(DayOfWeek.SUNDAY.get(ChronoField.DAY_OF_WEEK)); // 7
System.out.println(DayOfWeek.FRIDAY.isSupported(ChronoField.YEAR)); // false

try {
    final var ret = DayOfWeek.FRIDAY.get(ChronoField.YEAR);
} catch (UnsupportedTemporalTypeException e) {
    System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}

// Result
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: Year

String getDisplayName (TextStyle style, Locale locale)

Gets the textual representation, such as 'Mon' or 'Friday'.

final var locale = Locale.getDefault();
System.out.println(locale); // en_US

System.out.println(DayOfWeek.FRIDAY.getDisplayName(TextStyle.FULL, locale)); // Friday
System.out.println(DayOfWeek.FRIDAY.getDisplayName(TextStyle.SHORT, locale)); // Fri
System.out.println(DayOfWeek.FRIDAY.getDisplayName(TextStyle.NARROW, locale)); // F

long getLong (TemporalField field)

Gets the value of the specified field from this day-of-week as a long.

System.out.println(DayOfWeek.FRIDAY.isSupported(ChronoField.DAY_OF_WEEK)); // true
System.out.println(DayOfWeek.FRIDAY.getLong(ChronoField.DAY_OF_WEEK)); // 5

System.out.println(DayOfWeek.SUNDAY.isSupported(ChronoField.DAY_OF_WEEK)); // true
System.out.println(DayOfWeek.SUNDAY.getLong(ChronoField.DAY_OF_WEEK)); // 7

int getValue ()

Gets the day-of-week int value.

System.out.println(DayOfWeek.MONDAY.getValue()); // 1
System.out.println(DayOfWeek.TUESDAY.getValue()); // 2
System.out.println(DayOfWeek.WEDNESDAY.getValue()); // 3
System.out.println(DayOfWeek.THURSDAY.getValue()); // 4
System.out.println(DayOfWeek.FRIDAY.getValue()); // 5
System.out.println(DayOfWeek.SATURDAY.getValue()); // 6
System.out.println(DayOfWeek.SUNDAY.getValue()); // 7

boolean isSupported (TemporalField field)

Checks if the specified field is supported.

System.out.println(DayOfWeek.FRIDAY.isSupported(ChronoField.DAY_OF_WEEK)); // true
System.out.println(DayOfWeek.FRIDAY.get(ChronoField.DAY_OF_WEEK)); // 5

System.out.println(DayOfWeek.SUNDAY.isSupported(ChronoField.DAY_OF_WEEK)); // true
System.out.println(DayOfWeek.SUNDAY.get(ChronoField.DAY_OF_WEEK)); // 7
System.out.println(DayOfWeek.FRIDAY.isSupported(ChronoField.YEAR)); // false

try {
    final var ret = DayOfWeek.FRIDAY.get(ChronoField.YEAR);
} catch (UnsupportedTemporalTypeException e) {
    System.out.println("UnsupportedTemporalTypeException! : " + e.getMessage());
}

// Result
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: Year

DayOfWeek minus (long days)

Returns the day-of-week that is the specified number of days before this one.

final var week = DayOfWeek.FRIDAY;
System.out.println(week); // FRIDAY

final var ret1 = week.minus(1);
System.out.println(ret1); // THURSDAY

final var ret2 = week.minus(2);
System.out.println(ret2); // WEDNESDAY

final var ret3 = week.minus(6);
System.out.println(ret3); // SATURDAY

final var ret4 = week.minus(7);
System.out.println(ret4); // FRIDAY

static DayOfWeek of (int dayOfWeek)

Obtains an instance of DayOfWeek from an int value.

System.out.println(DayOfWeek.of(1)); // MONDAY
System.out.println(DayOfWeek.of(2)); // TUESDAY
System.out.println(DayOfWeek.of(3)); // WEDNESDAY
System.out.println(DayOfWeek.of(4)); // THURSDAY
System.out.println(DayOfWeek.of(5)); // FRIDAY
System.out.println(DayOfWeek.of(6)); // SATURDAY
System.out.println(DayOfWeek.of(7)); // SUNDAY

DayOfWeek plus (long days)

Returns the day-of-week that is the specified number of days after this one.

final var week = DayOfWeek.FRIDAY;
System.out.println(week); // FRIDAY

final var ret1 = week.plus(1);
System.out.println(ret1); // SATURDAY

final var ret2 = week.plus(2);
System.out.println(ret2); // SUNDAY

final var ret3 = week.plus(6);
System.out.println(ret3); // THURSDAY

final var ret4 = week.plus(7);
System.out.println(ret4); // FRIDAY

<R> R query (TemporalQuery<R> query)

Queries this day-of-week using the specified query.

final var ret = DayOfWeek.FRIDAY.query(TemporalQueries.precision());
System.out.println(ret); // Days

ValueRange range (TemporalField field)

Gets the range of valid values for the specified field.

final var ret = DayOfWeek.FRIDAY.range(ChronoField.DAY_OF_WEEK);
System.out.println(ret); // 1 - 7

static DayOfWeek valueOf (String name)

Returns the enum constant of this class with the specified name.

System.out.println(DayOfWeek.valueOf("FRIDAY")); // FRIDAY
System.out.println(DayOfWeek.valueOf("SATURDAY")); // SATURDAY
System.out.println(DayOfWeek.valueOf("SUNDAY")); // SUNDAY

static DayOfWeek[] values ()

Returns an array containing the constants of this enum class, in the order they are declared.

for (final var week : DayOfWeek.values()) {
    System.out.println(week);
}

// Result
// ↓
//MONDAY
//TUESDAY
//WEDNESDAY
//THURSDAY
//FRIDAY
//SATURDAY
//SUNDAY

Methods declared in Enum

clone, compareTo, describeConstable, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf

Please see the link below.


Related posts

To top of page