0

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

  1. 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
]
Anaya Adana
  • 135
  • 4

1 Answers1

0

A good alternative is to use the threeten backport - in Android here's how to configure it. Or, if you're using API level >= 26, just use the java.time classes.

This API makes things much easier. You can use the WeekFields class to define your week (starting on Friday):

// week starts on Friday
WeekFields wf = WeekFields.of(DayOfWeek.FRIDAY, 1);

The first parameter (DayOfWeek.FRIDAY) is the first day of the week, and the second parameter is the number of days in the first week. Check the documentation for more details about how these fields affect the class behaviour.

To get the current date, you can use the LocalDate class:

// current date
LocalDate dt = LocalDate.now();

Then you make a loop that subtracts a certain number of weeks, and use the WeekFields to get the first and last day of each week. I also used a DateTimeFormatter to print the dates to same format of your output:

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd-MMM", Locale.ENGLISH);
// get previous 4 weeks
for (int i = 4; i >= 1; i--) {
    LocalDate pastWeek = dt.minusWeeks(i);
    LocalDate startOfWeek = pastWeek.with(wf.dayOfWeek(), 1);
    LocalDate endOfWeek = pastWeek.with(wf.dayOfWeek(), 7);

    // you can add this String to your weekList
    String week = startOfWeek.format(fmt) + " to " + endOfWeek.format(fmt);
    System.out.println(week);
}

Output:

23-Feb to 01-Mar
02-Mar to 08-Mar
09-Mar to 15-Mar
16-Mar to 22-Mar

trinsh
  • 1