In this tutorial, we will learn how to get current and next week start and end date in Java using the LocalDate and DayOfWeek classes from the Java Time API .
- Create a model class called PlayWeek to store the week’s start and end dates.
- Declare two String variables: startDate and endDate.
class PlayWeek {
private String startDate;
private String endDate;
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public String toString() {
return "[startDate=" + startDate + ", endDate=" + endDate + "]";
}
}
- Create a model class named PlayWeeks. This class stores the details of the current week and the next week.
- Declare two PlayWeek reference variables: currentWeek and nextWeek.
class PlayWeeks {
private PlayWeek currentWeek;
private PlayWeek nextWeek;
public PlayWeek getCurrentWeek() {
return currentWeek;
}
public void setCurrentWeek(PlayWeek currentWeek) {
this.currentWeek = currentWeek;
}
public PlayWeek getNextWeek() {
return nextWeek;
}
public void setNextWeek(PlayWeek nextWeek) {
this.nextWeek = nextWeek;
}
public String toString() {
return "currentWeek=" + currentWeek + ", nextWeek=" + nextWeek;
}
}
- The getCurrentLocalDate() method retrieves the current date based on the configured time zone.
- The getWeeksWithoutDate() method is used to find the start and end dates of the current week and the next week.
- Inside this method, the current date is obtained by calling getCurrentLocalDate().
- The getDayOfWeek() method returns the current day of the week.
- The value of the current day is stored in valueOfCurrentDayOfWeek.
- Based on the value of valueOfCurrentDayOfWeek, the program calculates the starting date (currentSunday) of the current week using the if-else conditions.
- The starting date of the next week (nextSunday) is calculated by adding 7 days to currentSunday.
- The ending date of the current week (currentSaturday) is calculated by adding 6 days to currentSunday.
- The ending date of the next week (nextSaturday) is calculated by adding 6 days to nextSunday.
- A PlayWeek object named currentWeek is created, and the current week’s start and end dates are assigned to it.
- Another PlayWeek object named nextWeek is created, and the next week’s start and end dates are assigned to it.
- Both currentWeek and nextWeek are added to the PlayWeeks object.
- Now, the PlayWeeks object containing the current and next week’s details is returned.
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.TimeZone;
public class CurrentAndNextWeek {
private static final String ASIA_TIME_ZONE_ID = "Asia/Kolkata";
public static LocalDate getCurrentLocalDate() {
LocalDate currentDate = LocalDate.now(TimeZone.getTimeZone(ASIA_TIME_ZONE_ID).toZoneId());
return currentDate;
}
public static PlayWeeks getWeeksWithoutDate() {
PlayWeeks playWeeks = new PlayWeeks();
LocalDate currentDate = CurrentAndNextWeek.getCurrentLocalDate();
DayOfWeek currentDayOfWeek = currentDate.getDayOfWeek();
int valueOfCurrentDayOfWeek = currentDayOfWeek.getValue();
LocalDate currentSunday;
if (valueOfCurrentDayOfWeek == 1 || valueOfCurrentDayOfWeek == 2 || valueOfCurrentDayOfWeek == 3
|| valueOfCurrentDayOfWeek == 4) {
currentSunday = currentDate.minusDays(valueOfCurrentDayOfWeek + 2);
} else if (valueOfCurrentDayOfWeek == 5) {
currentSunday = currentDate;
} else if (valueOfCurrentDayOfWeek == 6) {
currentSunday = currentDate.minusDays(6);
} else {
currentSunday = currentDate.minusDays(1);
}
LocalDate nextSunday = currentSunday.plusDays(7);
LocalDate currentSaturday = currentSunday.plusDays(6);
LocalDate nextSaturday = nextSunday.plusDays(6);
PlayWeek currentWeek = new PlayWeek();
currentWeek.setStartDate(currentSunday.toString());
currentWeek.setEndDate(currentSaturday.toString());
playWeeks.setCurrentWeek(currentWeek);
PlayWeek nextWeek = new PlayWeek();
nextWeek.setStartDate(nextSunday.toString());
nextWeek.setEndDate(nextSaturday.toString());
playWeeks.setNextWeek(nextWeek);
return playWeeks;
}
public static void main(String[] args) {
System.out.println(getWeeksWithoutDate());
}
}
Output :- currentWeek=[startDate=2024-09-15, endDate=2024-09-21], nextWeek=[startDate=2024-09-22, endDate=2024-09-28]
Java Program to Calculate Current Week Dates
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class CurrentWeekDates {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
LocalDate startDate = currentDate
.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
LocalDate endDate = currentDate
.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY));
System.out.println("Current Week Start Date: " + startDate);
System.out.println("Current Week End Date: " + endDate);
}
}
Output :-
Current Week Start Date: 2026-09-13
Current Week End Date: 2026-09-19