Java Data Type How to - Get current week start and end date - (MONDAY TO SUNDAY)








Question

We would like to know how to get current week start and end date - (MONDAY TO SUNDAY).

Answer

import java.time.DayOfWeek;
import java.time.LocalDate;
/* www  .j ava 2s .  c  om*/
public class Main {
  public static void main(String[] args) {
    LocalDate today = LocalDate.now();

    // Go backward to get Monday
    LocalDate monday = today;
    while (monday.getDayOfWeek() != DayOfWeek.MONDAY) {
      monday = monday.minusDays(1);
    }

    // Go forward to get Sunday
    LocalDate sunday = today;
    while (sunday.getDayOfWeek() != DayOfWeek.SUNDAY) {
      sunday = sunday.plusDays(1);
    }

    System.out.println("Today: " + today);
    System.out.println("Monday of the Week: " + monday);
    System.out.println("Sunday of the Week: " + sunday);
  }
}

The code above generates the following result.