Android 日历 DAY_OF_WEEK 星期一返回“1”...但只有一次?
Android Calendar DAY_OF_WEEK returned '1' for monday... but only once?
我有一个使用闹钟来启动无线电流的应用程序。它有一个 'repeat daily' 功能。为了检查警报是否应在特定日期触发,我检查 'DAY_OF_WEEK' 是否在数组中。像这样:
int[] repeatOnDays = [0,1,1,1,1,1,1]; // first nr is sunday, last is saturday
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1; // -1 because Sunday==1 but its index in the array is 0
if (repeatOnDays[dayOfWeek]>0) { /* FIRE ALARM TODAY */ }
else { /* DON'T FIRE ALARM TODAY */ }
(注意:上面的代码可能不是 100% java,我已经简化了一些东西)
今天早上,当我的代码 运行 时,它说 dayOfWeek 是“0”(星期日),但它是星期一!当我设置另一个闹钟时,它突然说 dayOfWeek 是“1”。
什么?怎么会这样?
// 更新:这是实际代码:
JSONArray repeatDaily = new JSONArray("[0,1,1,1,1,1,1]"); // <- This is not actually here but it may help read the rest of the code :)
boolean fireToday = true;
if (repeat.equals("daily")) {
Log.d(APPTAG," > Daily repeat..");
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK)-1;
if (repeatDaily.length()<dayOfWeek) { fireToday = false; }
else if (repeatDaily.getInt(dayOfWeek)>0) { fireToday = true; }
else { fireToday = false; }
Log.d(APPTAG," > Day: "+ dayOfWeek +", "+ repeatDaily.getInt(dayOfWeek));
}
if (!fireToday) {
Log.d(APPTAG," > Do not need to fire today");
return; // <-- important stuff
}
Logcat:
06:30:01 D/AlarmMgr > Daily repeat..
06:30:01 D/AlarmMgr > Day: 0, 0
06:30:01 D/AlarmMgr > Do not need to fire today
看来是我自己的错。在我粘贴的代码之前的几行中,我设置了日历的时间:
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timeInMs);
但我有一些奇怪的代码,在某些情况下,这些代码会导致 timeInMs 成为过去的日期。
改为
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
应该可以解决它 :P
我有一个使用闹钟来启动无线电流的应用程序。它有一个 'repeat daily' 功能。为了检查警报是否应在特定日期触发,我检查 'DAY_OF_WEEK' 是否在数组中。像这样:
int[] repeatOnDays = [0,1,1,1,1,1,1]; // first nr is sunday, last is saturday
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1; // -1 because Sunday==1 but its index in the array is 0
if (repeatOnDays[dayOfWeek]>0) { /* FIRE ALARM TODAY */ }
else { /* DON'T FIRE ALARM TODAY */ }
(注意:上面的代码可能不是 100% java,我已经简化了一些东西)
今天早上,当我的代码 运行 时,它说 dayOfWeek 是“0”(星期日),但它是星期一!当我设置另一个闹钟时,它突然说 dayOfWeek 是“1”。
什么?怎么会这样?
// 更新:这是实际代码:
JSONArray repeatDaily = new JSONArray("[0,1,1,1,1,1,1]"); // <- This is not actually here but it may help read the rest of the code :)
boolean fireToday = true;
if (repeat.equals("daily")) {
Log.d(APPTAG," > Daily repeat..");
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK)-1;
if (repeatDaily.length()<dayOfWeek) { fireToday = false; }
else if (repeatDaily.getInt(dayOfWeek)>0) { fireToday = true; }
else { fireToday = false; }
Log.d(APPTAG," > Day: "+ dayOfWeek +", "+ repeatDaily.getInt(dayOfWeek));
}
if (!fireToday) {
Log.d(APPTAG," > Do not need to fire today");
return; // <-- important stuff
}
Logcat:
06:30:01 D/AlarmMgr > Daily repeat..
06:30:01 D/AlarmMgr > Day: 0, 0
06:30:01 D/AlarmMgr > Do not need to fire today
看来是我自己的错。在我粘贴的代码之前的几行中,我设置了日历的时间:
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timeInMs);
但我有一些奇怪的代码,在某些情况下,这些代码会导致 timeInMs 成为过去的日期。
改为
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
应该可以解决它 :P