[Java] [Java] Get the day of a specific day of the week

less than 1 minute read

As a memorandum

Easy to use LocalDate.


// today
LocalDate now = LocalDate.now(); // 2020-06-23: Tuesday

// this Friday
LocalDate thisFriday = now.with(DayOfWeek.FRIDAY); // 2020-06-26

// last Wednesday
LocalDate lastWednesDay = now.minusWeeks(1).with(DayOfWeek.WEDNESDAY); // 2020-06-17

// next Thursday
LocalDate nextThursDay = now.plusWeeks(1).with(DayOfWeek.THURSDAY); // 2020-07-02

// Extra conversion to LocalDateTime
LocalDateTime from = lastWednesDay.atTime(00, 00, 00); // 2020-06-17 00:00:00

Reference

Date and Time API Tips for Japanese (ja) [Java] Acquiring Monday and Sunday dates of the past week in order @kazokclily

Tags:

Updated: