Nezam - 8 months ago 40

Android Question

i want to get number of weeks in current month.That including the previous month's days in the last week and the first week days in the next month.

Something like this:

I want to use this in a certain gridview adapter on android and am trying to manipulate this code block:

`//FIRST_DAY_OF_WEEK =0;`

int lastDay = month.getActualMaximum(Calendar.DAY_OF_MONTH);

int firstDay = (int) month.get(Calendar.DAY_OF_WEEK);

int firstDayNextMonth = nextMonth.get(Calendar.DAY_OF_WEEK);

int lastDayNextMonth=nextMonth.getActualMaximum(Calendar.DAY_OF_MONTH);

int nextMonthdays=nextMonth.getMinimalDaysInFirstWeek();

// figure size of the array

if (firstDay == 1) {

days = new String[lastDay + (FIRST_DAY_OF_WEEK * 6)];

}

else {

days = new String[lastDay + firstDay - (FIRST_DAY_OF_WEEK + 1)];

}

Answer

The WEEK_OF_YEAR attribute of the Calendar class can be useful for you. Ref: Calendar class

Create a new date that will be the first day of the some month. Get the week of the year for this day, let say you got start value.

Create a new date that will be the last day of the given month. Get the week of the year for this day, so now you got end value.

Now end - start + 1 should be one that you want.

You may have to handle some corner case when the week overlaps to another year or similar. But I think once you get this right, you can do that with simple logic.

Here is simple example code. I think you can make it into function and pass whatever you want.

```
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2013);
cal.set(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, 1);
int start = cal.get(Calendar.WEEK_OF_YEAR);
Log.d("BLA BLA", "Value is " + start);
cal.set(Calendar.YEAR, 2013);
cal.set(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, 28);
int end = cal.get(Calendar.WEEK_OF_YEAR);
Log.d("BLA BLA", "Value is " + end);
Log.d("BLA BLA", "Num weeks:: " + (end - start +1 ));
```

**For Corner case:**

Corner case will only occur for the month of Januaray (E.g Jan 2010, Jan 2000) in those cases most of the days are part of the last week of the previous year so the start value will be 52 and end value will be 5. When this occurs check if,

```
if(start > end) {
numweeks = end + 1;
}
```

P.S: Put it to different testing inputs that you got. Let me know If I can improve on it once you find any fault.

**And Even much simpler solution:**

```
Calendar cal = Calendar.getInstance();
for(int i = 0 ; i < 11;i++){
cal.set(Calendar.YEAR, 2013);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.MONTH, i);
int maxWeeknumber = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);
Log.d("LOG","max week number" + maxWeeknumber);
}
01-22 01:49:03.591: D/LOG(573): max week number5
01-22 01:49:03.602: D/LOG(573): max week number5
01-22 01:49:03.602: D/LOG(573): max week number6
01-22 01:49:03.671: D/LOG(573): max week number5
01-22 01:49:03.671: D/LOG(573): max week number5
01-22 01:49:03.671: D/LOG(573): max week number6
01-22 01:49:03.681: D/LOG(573): max week number5
01-22 01:49:03.691: D/LOG(573): max week number5
01-22 01:49:03.691: D/LOG(573): max week number5
01-22 01:49:03.711: D/LOG(573): max week number5
01-22 01:49:03.711: D/LOG(573): max week number5
```

**Simple solution works fine with corner cases:**

```
Calendar cal = Calendar.getInstance();
for(int i = 0 ; i < 11;i++){
cal.set(Calendar.YEAR, 2010);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.MONTH, i);
int maxWeeknumber = cal.getActualMaximum(Calendar.WEEK_OF_MONTH);
// Month value starts from 0 to 11 for Jan to Dec
Log.d("LOG","For Month :: "+ i + " Num Week :: " + maxWeeknumber);
}
```

Log:

```
01-22 01:59:35.841: D/LOG(629): For Month :: 0 Num Week :: 6
01-22 01:59:35.841: D/LOG(629): For Month :: 1 Num Week :: 5
01-22 01:59:35.841: D/LOG(629): For Month :: 2 Num Week :: 5
01-22 01:59:35.841: D/LOG(629): For Month :: 3 Num Week :: 5
01-22 01:59:35.841: D/LOG(629): For Month :: 4 Num Week :: 6
01-22 01:59:35.852: D/LOG(629): For Month :: 5 Num Week :: 5
01-22 01:59:35.871: D/LOG(629): For Month :: 6 Num Week :: 5
01-22 01:59:35.871: D/LOG(629): For Month :: 7 Num Week :: 5
01-22 01:59:35.871: D/LOG(629): For Month :: 8 Num Week :: 5
01-22 01:59:35.871: D/LOG(629): For Month :: 9 Num Week :: 6
01-22 01:59:35.871: D/LOG(629): For Month :: 10 Num Week :: 5
```