Android Java Get Week Numbers Of A Certain Month

To get a week number of a specific date use the Calendar.Week_of_year. To get the week numbers of a certain month run through each day of the month. You can do something like this, calling the function for a the specific month and year and String[] in return with the week number for each day.

public static String[] getWeeksOfMonth(int month, int year) {
 Calendar cal = Calendar.getInstance();
 cal.set(Calendar.YEAR, year);
 cal.set(Calendar.MONTH, month);
 cal.set(Calendar.DAY_OF_MONTH, 1);
int ndays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
 String weeks[] = new String[ndays];
 for (int i = 0; i < ndays; i++) {
 weeks[i] = String.format("%d", cal.get(Calendar.WEEK_OF_YEAR));
 cal.add(Calendar.DATE, 1);
 }
return weeks;
}

ndays is the numbers of days in the month from the getActualMaximum (28, 30 or 31). Then running through each day and returning the week string[]

Posted in Android, Tips and Tricks Tagged with: , , ,