Java : DayOfWeek (曜日) - API使用例
DayOfWeek (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
DayOfWeek は曜日を表す 列挙型 です。
final var date = LocalDate.of(2100, 12, 1);
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
System.out.println(date.format(formatter)); // 2100年12月1日水曜日
final var week = date.getDayOfWeek();
System.out.println(week); // WEDNESDAY
列挙型定数
FRIDAY
System.out.println(DayOfWeek.FRIDAY); // FRIDAY
final var name = DayOfWeek.FRIDAY.getDisplayName(TextStyle.FULL, Locale.JAPAN);
System.out.println(name); // 金曜日
MONDAY
System.out.println(DayOfWeek.MONDAY); // MONDAY
final var name = DayOfWeek.MONDAY.getDisplayName(TextStyle.FULL, Locale.JAPAN);
System.out.println(name); // 月曜日
SATURDAY
System.out.println(DayOfWeek.SATURDAY); // SATURDAY
final var name = DayOfWeek.SATURDAY.getDisplayName(TextStyle.FULL, Locale.JAPAN);
System.out.println(name); // 土曜日
SUNDAY
System.out.println(DayOfWeek.SUNDAY); // SUNDAY
final var name = DayOfWeek.SUNDAY.getDisplayName(TextStyle.FULL, Locale.JAPAN);
System.out.println(name); // 日曜日
THURSDAY
System.out.println(DayOfWeek.THURSDAY); // THURSDAY
final var name = DayOfWeek.THURSDAY.getDisplayName(TextStyle.FULL, Locale.JAPAN);
System.out.println(name); // 木曜日
TUESDAY
System.out.println(DayOfWeek.TUESDAY); // TUESDAY
final var name = DayOfWeek.TUESDAY.getDisplayName(TextStyle.FULL, Locale.JAPAN);
System.out.println(name); // 火曜日
WEDNESDAY
System.out.println(DayOfWeek.WEDNESDAY); // WEDNESDAY
final var name = DayOfWeek.WEDNESDAY.getDisplayName(TextStyle.FULL, Locale.JAPAN);
System.out.println(name); // 水曜日
メソッド
Temporal adjustInto (Temporal temporal)
final var temporal = LocalDate.of(2100, 12, 17);
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
System.out.println(temporal.format(formatter)); // 2100年12月17日金曜日
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)
final var temporal = LocalDate.of(2100, 12, 17);
final var formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
System.out.println(temporal.format(formatter)); // 2100年12月17日金曜日
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)); // 2100年12月18日土曜日
System.out.println(DayOfWeek.from(temporal)); // SATURDAY
int get (TemporalField field)
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());
}
// 結果
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: Year
String getDisplayName (TextStyle style, Locale locale)
final var locale = Locale.getDefault();
System.out.println(locale); // ja_JP
System.out.println(DayOfWeek.FRIDAY.getDisplayName(TextStyle.FULL, locale)); // 金曜日
System.out.println(DayOfWeek.FRIDAY.getDisplayName(TextStyle.SHORT, locale)); // 金
System.out.println(DayOfWeek.FRIDAY.getDisplayName(TextStyle.NARROW, locale)); // 金
long getLong (TemporalField field)
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 ()
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)
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());
}
// 結果
// ↓
//UnsupportedTemporalTypeException! : Unsupported field: Year
DayOfWeek minus (long days)
final var week = DayOfWeek.FRIDAY;
System.out.println(week.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 金曜日
final var ret1 = week.minus(1);
System.out.println(ret1.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 木曜日
final var ret2 = week.minus(2);
System.out.println(ret2.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 水曜日
final var ret3 = week.minus(6);
System.out.println(ret3.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 土曜日
final var ret4 = week.minus(7);
System.out.println(ret4.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 金曜日
static DayOfWeek of (int dayOfWeek)
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)
final var week = DayOfWeek.FRIDAY;
System.out.println(week.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 金曜日
final var ret1 = week.plus(1);
System.out.println(ret1.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 土曜日
final var ret2 = week.plus(2);
System.out.println(ret2.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 日曜日
final var ret3 = week.plus(6);
System.out.println(ret3.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 木曜日
final var ret4 = week.plus(7);
System.out.println(ret4.getDisplayName(TextStyle.FULL, Locale.JAPAN)); // 金曜日
<R> R query (TemporalQuery<R> query)
final var ret = DayOfWeek.FRIDAY.query(TemporalQueries.precision());
System.out.println(ret); // Days
ValueRange range (TemporalField field)
final var ret = DayOfWeek.FRIDAY.range(ChronoField.DAY_OF_WEEK);
System.out.println(ret); // 1 - 7
static DayOfWeek valueOf (String 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 ()
for (final var week : DayOfWeek.values()) {
System.out.println(week);
}
// 結果
// ↓
//MONDAY
//TUESDAY
//WEDNESDAY
//THURSDAY
//FRIDAY
//SATURDAY
//SUNDAY
Enumで宣言されたメソッド
clone, compareTo, describeConstable, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
「Java API 使用例 : Enum」をご参照ください。
関連記事
- 日付・時刻の基本
- Date, CalendarではなくLocalDateTime, ZonedDateTimeを使おう
- 文字列と日付・時刻の変換
- 日付と時刻、曜日の計算
- 現在時刻(日時)の取得いろいろ
- 現在の曜日(DayOfWeek)を取得
- ZoneIdとZoneOffsetの違い
- API 使用例
- Calendar (カレンダー)
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock (時計)
- Date (日付・時刻)
- DateTimeException (日付・時刻の例外)
- DateTimeParseException (日付・時刻の解析例外)
- Duration (時間の量)
- Era (紀元)
- Instant (時点)
- InstantSource
- JapaneseDate (和暦を使った日付)
- LocalDate (日付・タイムゾーンなし)
- LocalDateTime (日時・タイムゾーンなし)
- LocalTime (時刻・タイムゾーンなし)
- Month (月)
- MonthDay (月・日)
- OffsetDateTime (日時・オフセットあり)
- OffsetTime (時刻・オフセットあり)
- Period (日付の量)
- Temporal
- TemporalAccessor
- TemporalAdjuster (日付・時刻の調整)
- TemporalAdjusters (日付・時刻の調整ユーティリティ)
- TimeZone (タイムゾーン)
- Year (年)
- YearMonth (年・月)
- ZonedDateTime (日時・タイムゾーンあり)
- ZoneId (タイムゾーンID)
- ZoneOffset (タイムゾーン・オフセット)