我正在尝试确定一周中的哪一天是每月的第一天,但由于某种原因,它没有返回正确的一周中的哪一天。
下面是我的代码:
CalendarMonth[] months = CalendarUtils.constructMonthViewArray(new GregorianCalendar());
public static CalendarMonth[] constructMonthViewArray(Calendar cal) {
CalendarMonth[] months = new CalendarMonth[CALENDAR_GRID_SIZE];
int year = cal.get(cal.YEAR);
int month = cal.get(cal.MONTH);;
// calculate how many days in the month
int numOfDays = getNumOfDaysInMonth(cal);
// calculate what day(mon-sunday) is the 1st of the month
int firstDayOfMonth = getFirstDayOfMonth(cal);
private static int getFirstDayOfMonth(Calendar cal) {
int firstDay = cal.get(Calendar.DAY_OF_WEEK);
Log.d(TAG, "");
// decrement it because our array deals with values 0-6(indexes)
firstDay--;
if (firstDay == 0) {
firstDay = 6;
} else {
// decrement again so that the days start from 0.
firstDay--;
}
return firstDay;
}
"int firstDay = cal.get(Calendar.DAY_OF_WEEK);“中的这一行没有给出正确的星期几,返回值2,表示当月的第一天在星期六(7)时获得本月的第一天(2011年1月)。
我是不是遗漏了什么?我已经调试和检查了cal变量设置的月份、年份和日期,它确实表明今天的日期是正确的,但当我得到星期几时,它不会得到值7。
发布于 2011-01-10 18:18:26
getFirstDayOfMonth()的代码看起来根本不像方法名所说的那样--但要真正理解发生了什么,我们需要查看所有涉及到的代码,特别是日历是如何初始化的以及getNumOfDaysInMonth()的代码。
要记住的一件事是:根据getFirstDayOfWeek()
,一周的第一天是什么取决于区域设置
https://stackoverflow.com/questions/4645608
复制相似问题