My week start from Friday and end from Thursday, and i get a list of weeks of current month weeks. In my Code every thing working fine but get current month four weeks but i want previous four weeks not next weeks of current week.
public void getWeeksOfMonth( int year) {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
Calendar cal = Calendar.getInstance();
int currentmonth = cal.get(Calendar.MONTH);
int val = 0;
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, currentmonth);
cal.set(DAY_OF_MONTH, 1);
int ndays = cal.getActualMaximum(DAY_OF_MONTH);
System.out.println(ndays + "<<<ff");
while (cal.get(DAY_OF_WEEK) != FRIDAY) {
cal.add(DAY_OF_MONTH, 1);
ndays--;
}
int remainingDays = ndays % 7;
if (remainingDays == 0)
ndays += 7;
else
ndays = ndays + 7 - remainingDays;
int inc = 1;
for (int i = 1; i <= ndays; i++) {
String day = sdf.format(cal.getTime());
System.out.println(day + "<<<");
Log.e("quest", day + "<<<");
inc++;
if (val == 0) {
firstweek = day.substring(0, 6);
// weeklist.add(firstweek);
val = 1;
}
if (i % 7 == 0) {
String s = day.substring(0, 6);
weeklist.add(firstweek + " to " + s);
val = 0;
Log.e("weekdayss", "=======week days===========" + weeklist);
inc = 0;
}
if (inc >= 1 && i == ndays) {
for (int ii = inc; ii <= 6; ii++) {
String dayi = sdf.format(cal.getTime());
System.out.println(dayi + "<<<");
Log.e("quest1", dayi + "<<<");
inc++;
}
}
cal.add(Calendar.DATE, 1);
}
if (weeklist.size() == 5) {
weeklist.remove(4);
}
if (weeklist.size() == 6) {
weeklist.remove(5);
weeklist.remove(4);
}
}
Problem
- Want to get previous four weeks, not current Month four weeks
OUTPUT
[
02-Mar to 08-Mar
09-Mar to 15-Mar
16-Mar to 22-Mar
23-Mar to 29-Mar
]