It does not take much code to do this with date4j. Example of calculating the first day of the week:
private void firstDayOfThisWeek(){ DateTime today = DateTime.today(TimeZone.getDefault()); DateTime firstDayThisWeek = today; //start value int todaysWeekday = today.getWeekDay(); int SUNDAY = 1; if(todaysWeekday > SUNDAY){ int numDaysFromSunday = todaysWeekday - SUNDAY; firstDayThisWeek = today.minusDays(numDaysFromSunday); } System.out.println("The first day of this week is : " + firstDayThisWeek); }
The foregoing follows that Sunday is the beginning of the week. Some jurisdictions do not apply this agreement, so you will need to change the code for such cases.
source share