There are following way to get next week and current week starting and ending dates in java.
- Create a model class name PlayWeek.
- Declare two variable String type startDate and end Date.
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 name PlayWeeks, here we find current and next week.
- Declare a PlayWeek class reference type with currentWeek and next week variable.
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;
}
}
- From getCurrentPDTLocalDate() method we find current date of Asia time zone.
- Create a method getWeeksWithoutDate() to find next and current week date.
- Inside if conditions,we find out current week at depend on the valueOfCurrentDayOfWeek.
- To find next week we add 7 days to current week.
- Now add the current and next date to PlayWeeks and return the response.
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.TimeZone;
public class CurrentAndNextWeek {
private static final String ASIA_TIME_ZONE_ID = "India/Mumbai";
public static LocalDate getCurrentPDTLocalDate() {
LocalDate currentDate = LocalDate.now(TimeZone.getTimeZone(ASIA_TIME_ZONE_ID).toZoneId());
return currentDate;
}
public static PlayWeeks getWeeksWithoutDate() {
PlayWeeks playWeeks = new PlayWeeks();
LocalDate currentDate = CurrentAndNextWeek.getCurrentPDTLocalDate();
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]