Calendar.setFirstDayOfWeek() 这个方法在调用的时候,应该在 Calendar.setTimeInMillis() 之前,否则不会生效,原因是 setFirstDayOfWeek() 这个方法在 Android SDK 中的实现只是做了参数的设置,并没有进行刷新(没有调用 computeFields() 方法),JDK 中的这个方法调用了刷新方法,下面是这一方法在 SDK 和 JDK 中的区别。
在 Android SDK 中的实现如下:
/**
* Sets the first day of the week for this {@code Calendar}.
* The value should be a day of the week such as {@code MONDAY}.
*/
public void setFirstDayOfWeek(int value) {
firstDayOfWeek = value;
}
在 JDK 中的实现如下:
/**
* Sets this Calendar's current time from the given long value.
*
* @param millis the new time in UTC milliseconds from the epoch.
* @see #setTime(Date)
* @see #getTimeInMillis()
*/
public void setTimeInMillis(long millis) {
// If we don't need to recalculate the calendar field values,
// do nothing.
if (time == millis && isTimeSet && areFieldsSet && areAllFieldsSet
&& (zone instanceof ZoneInfo) && !((ZoneInfo)zone).isDirty()) {
return;
}
time = millis;
isTimeSet = true;
areFieldsSet = false;
computeFields();
areAllFieldsSet = areFieldsSet = true;
}
正确的使用方式:
public static Integer parseWorkReportMonthly(long date) {
Calendar calendar = getCalendar();
//这里的设置有意义
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTimeInMillis(date);
//这里设置没有意义
// calendar.setFirstDayOfWeek(Calendar.SUNDAY);
return calendar.get(Calendar.WEEK_OF_MONTH);
}