我在我的应用程序中使用了自定义日历。我已为用户提供了选择一周中第一天的选项,可能是:Saturday, Sunday, Monday
Saturday, Sunday, Monday
我想获取一个月中的星期数-取决于星期几的开始时间,而不是星期几的默认值- Sunday。
Sunday
码:
public int getWeeksOfMonth(int year, int month) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MONTH, month-1); calendar.set(Calendar.YEAR, year); int numOfWeeksInMonth = calendar.getActualMaximum(Calendar.WEEK_OF_MONTH); return numOfWeeksInMonth; }
尝试
calendar.setFirstDayOfWeek(Calendar.MONDAY); //sets first day to monday, for example.
在您的情况下,您可能需要执行以下操作:
public int getWeeksOfMonth(int year, int month, int weekStart) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MONTH, month-1); calendar.set(Calendar.YEAR, year); calendar.setFirstDayOfWeek(weekStart); int numOfWeeksInMonth = calendar.getActualMaximum(Calendar.WEEK_OF_MONTH); return numOfWeeksInMonth; }
并用诸如
int weeks = getWeeksOfMonth(2012, 5, Calendar.WEDNESDAY);