返回了错误的 week_of_month 和 day_of_month
Wrong week_of_month and day_of_month returned
这是我的代码:
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(Calendar.WEEK_OF_YEAR, selectedWeek);
c.set(Calendar.YEAR, selectedYear);
Log.v(FILE_NAME,"C content: " + c.toString());
int firstDayOfWeek = c.getFirstDayOfWeek();
String[] days = new String[firstDayOfWeek + 7];
for (int i = firstDayOfWeek; i < firstDayOfWeek + 7; i++) {
c.set(Calendar.DAY_OF_WEEK, i);
days[i] = new SimpleDateFormat("d MMM").format(c.getTime());
}
Log.v(FILE_NAME, "Start " + days[firstDayOfWeek] + " to " + days[firstDayOfWeek + 6]);
我将 selectedWeek 设置为 51,将 selectedYear 设置为 2016。所以我的期望是,因为我已经给出了周和年,在我最后 Log.v 中,我应该得到给定周的日期列表(51),即 12 月 19 日至 12 月 25 日。相反,我将始终获取当前周的日期(例如,目前我将始终获取 12 月 12 日至 12 月 18 日)
显示c的内容给我
java.util.GregorianCalendar[time=?,areFieldsSet=false,lenient=true,zone=Asia/Kuala_Lumpur,firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2016,MONTH=11,WEEK_OF_YEAR=52,WEEK_OF_MONTH=3,DAY_OF_MONTH=18,DAY_OF_YEAR=353,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=1,HOUR_OF_DAY=1,MINUTE=24,SECOND=46,MILLISECOND=30,ZONE_OFFSET=28800000,DST_OFFSET=0]
似乎 WEEK_OF_MONTH return 是 CURRENT WEEK_OF_MONTH,而不是给定的 selectedWeek 和 selectedYear。 DAY_OF_MONTH 也是如此。我的假设是 DAY_OF_MONTH 至少应该 return 给定 selectedWeek 的开始日期?
我是不是理解错了?如果我这样做了,我应该怎么做才能使第几天 [firstDayOfWeek] return 成为给定周和年的第一天?
我想我找到了问题所在。问题是当您设置 Calendar.DAY_OF_WEEK
时,日历会自行重置,只需将星期几更改为您设置的日期。
因此,为了让您的代码正常工作,您必须在 for 循环中设置 Calendar.WEEK_OF_YEAR
。
for (int i = firstDayOfWeek; i < firstDayOfWeek + 7; i++) {
c.set(Calendar.DAY_OF_WEEK, i);
c.set(Calendar.WEEK_OF_YEAR, selectedWeek);
days[i] = new SimpleDateFormat("d MMM").format(c.getTime());
}
这是我的代码:
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(Calendar.WEEK_OF_YEAR, selectedWeek);
c.set(Calendar.YEAR, selectedYear);
Log.v(FILE_NAME,"C content: " + c.toString());
int firstDayOfWeek = c.getFirstDayOfWeek();
String[] days = new String[firstDayOfWeek + 7];
for (int i = firstDayOfWeek; i < firstDayOfWeek + 7; i++) {
c.set(Calendar.DAY_OF_WEEK, i);
days[i] = new SimpleDateFormat("d MMM").format(c.getTime());
}
Log.v(FILE_NAME, "Start " + days[firstDayOfWeek] + " to " + days[firstDayOfWeek + 6]);
我将 selectedWeek 设置为 51,将 selectedYear 设置为 2016。所以我的期望是,因为我已经给出了周和年,在我最后 Log.v 中,我应该得到给定周的日期列表(51),即 12 月 19 日至 12 月 25 日。相反,我将始终获取当前周的日期(例如,目前我将始终获取 12 月 12 日至 12 月 18 日)
显示c的内容给我
java.util.GregorianCalendar[time=?,areFieldsSet=false,lenient=true,zone=Asia/Kuala_Lumpur,firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2016,MONTH=11,WEEK_OF_YEAR=52,WEEK_OF_MONTH=3,DAY_OF_MONTH=18,DAY_OF_YEAR=353,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=1,HOUR_OF_DAY=1,MINUTE=24,SECOND=46,MILLISECOND=30,ZONE_OFFSET=28800000,DST_OFFSET=0]
似乎 WEEK_OF_MONTH return 是 CURRENT WEEK_OF_MONTH,而不是给定的 selectedWeek 和 selectedYear。 DAY_OF_MONTH 也是如此。我的假设是 DAY_OF_MONTH 至少应该 return 给定 selectedWeek 的开始日期?
我是不是理解错了?如果我这样做了,我应该怎么做才能使第几天 [firstDayOfWeek] return 成为给定周和年的第一天?
我想我找到了问题所在。问题是当您设置 Calendar.DAY_OF_WEEK
时,日历会自行重置,只需将星期几更改为您设置的日期。
因此,为了让您的代码正常工作,您必须在 for 循环中设置 Calendar.WEEK_OF_YEAR
。
for (int i = firstDayOfWeek; i < firstDayOfWeek + 7; i++) {
c.set(Calendar.DAY_OF_WEEK, i);
c.set(Calendar.WEEK_OF_YEAR, selectedWeek);
days[i] = new SimpleDateFormat("d MMM").format(c.getTime());
}