Get week range of current month in Android

243 Views Asked by At

I would like to show the week range in the MPAndroidChart Graph. For example in February month I would like to show the xAxis label value like 1-4, 5-11, 12- 18, 19-25, 26-28. Here 1-4 comes from the 1st week of February where previous month dates also availble. But I need only current month days. However I am getting all the dates in the week.

 public List<String> getWeeksInCurrentMonth() {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DAY_OF_MONTH, 1);
    int month = cal.get(Calendar.MONTH);

    List<String> weekRanges = new ArrayList<>();

    while (cal.get(Calendar.MONTH) == month) {
        int week = cal.get(Calendar.WEEK_OF_MONTH);
        int year = cal.get(Calendar.YEAR);
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

        if (dayOfWeek == Calendar.SUNDAY || cal.getActualMaximum(Calendar.DAY_OF_MONTH) == cal.get(Calendar.DAY_OF_MONTH)) {
            int startDay = cal.get(Calendar.DAY_OF_MONTH) - (dayOfWeek - 1);
            int endDay = cal.get(Calendar.DAY_OF_MONTH) + (7 - dayOfWeek);

            if (endDay > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
                endDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            }

            if (startDay <= endDay && startDay <= cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
                weekRanges.add(String.format("%d-%d", startDay, endDay));
            }
        }

        cal.add(Calendar.DAY_OF_MONTH, 1);
    }

    System.out.println(weekRanges);
    return weekRanges;

 }

Observed output when run in US locale:

[5-11, 12-18, 19-25, 26-28, 26-28]

It seems that the first week is missing.

Someone please shed some light here, to acheive the week range with only current month date.

1

There are 1 best solutions below

2
Lishchuk Bohdan On

To get the week ranges with only current month dates, you can modify the logic in your getWeeksInCurrentMonth() method to take the current month into account and exclude any dates from previous or next months. Here's an updated implementation that should achieve the desired behavior:

public List<String> getWeeksInCurrentMonth() {
    Calendar cal = Calendar.getInstance();
    int currentMonth = cal.get(Calendar.MONTH);
    int currentYear = cal.get(Calendar.YEAR);
    cal.set(Calendar.DAY_OF_MONTH, 1);

    List<String> weekRanges = new ArrayList<>();

    while (cal.get(Calendar.MONTH) == currentMonth) {
        int week = cal.get(Calendar.WEEK_OF_MONTH);
        int year = cal.get(Calendar.YEAR);
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

        // Only consider days in the current month
        if (cal.get(Calendar.MONTH) == currentMonth && cal.get(Calendar.YEAR) == currentYear) {
            int startDay = cal.get(Calendar.DAY_OF_MONTH);
            int endDay = startDay + (7 - dayOfWeek);

            if (endDay > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
                endDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            }

            weekRanges.add(String.format("%d-%d", startDay, endDay));
        }

        cal.add(Calendar.DAY_OF_MONTH, 7 - dayOfWeek + 1);
    }

    System.out.println(weekRanges);
    return weekRanges;
}