Once a Time I had a Scenario where I should display a Page with List of employees movement from Last Friday to this week Saturday if Saturday is over(you are in Sunday).If you are in any other day other than Saturday(Mon..Fri) then you should choose date of Last before Saturday to Friday.
The above would be made with the following
As you can see in the above diagram I have added two calendar.
In First I should Get Date between 15 to 21 since today date is this 28 and Saturday is not over
In Second I should Get Date between 22 to 28 since today date is this 30 nd Saturday is over
java.util.Date dt = new java.util.Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String currentTime = sdf.format(dt); Calendar cal = Calendar.getInstance(); int daysBackToSat = cal.get(Calendar.DAY_OF_WEEK); //Checks if this weeks friday is Over.in My Case 28 falls on Friday. //Returns 6 the below case is True so adds 13 to get before Saturday if(daysBackToSat < 7) daysBackToSat += 7; //6+7 //Subtracting 13 Days for Last Saturday //Since this Saturday is not Over cal.add(Calendar.DATE, daysBackToSat * -1); System.out.println("Start Date" + sdf.format(cal.getTime())); cal.add(Calendar.DATE, 6); System.out.println("End Date" + sdf.format(cal.getTime()));
Output Code
2013-06-15
2013-06-21