LoginSignup
2
1

More than 3 years have passed since last update.

【Java】特定の曜日の日を取得する

Posted at

忘備録として

LocalDateを使うとかんたん。


// 今日
LocalDate now = LocalDate.now();    // 2020-06-23:火曜日

// 今週の金曜日
LocalDate thisFriday = now.with(DayOfWeek.FRIDAY);    // 2020-06-26

// 先週の水曜日
LocalDate lastWednesDay = now.minusWeeks(1).with(DayOfWeek.WEDNESDAY);    // 2020-06-17

// 来週の木曜日
LocalDate nextThursDay = now.plusWeeks(1).with(DayOfWeek.THURSDAY);    // 2020-07-02

// おまけ LocalDateTimeへ変換
LocalDateTime from = lastWednesDay.atTime(00, 00, 00);    // 2020-06-17 00:00:00

参考

日本人のための Date and Time API Tips (ja)
【Java】過去週の月曜日と日曜日の日付を順番に取得する @kazokclily

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1