我知道这有点晚了,但我有一个非常相似的问题并且很难找到我正在寻找的答案。全天事件的强制 UTC 时区使事情变得棘手。这是我的解决方案:
// "allDayStart" is an all-day event today, encoded in the default time zone
Time allDayStart = new Time();
allDayStart.timezone=TimeZone.getDefault().toString();
allDayStart.set(dayStart.monthDay, dayStart.month, dayStart.year);
// 2 time selections for the query:
// 1) Between day-start and day-end (not all-day); or
// 2) Equals today at 0:00:00 (all-day) in the default timezone
String calSelection =
"((" + Calendars.ACCOUNT_NAME + " = ?) " +
"AND (" + Calendars.OWNER_ACCOUNT + "= ?) " +
"AND (" +
"((" + Events.DTSTART + ">= ?) " +
"AND (" + Events.DTSTART + "<= ?) " +
"AND (" + Events.ALL_DAY + "= ?) " +
") " +
"OR ((" + Events.DTSTART + "= ?) " +
"AND (" + Events.ALL_DAY + "= ?)" +
")" +
")" +
")";
String[] calSelectionArgs = new String[] {
accountName, ownerName,
dayStartInMillis.toString(), dayEndInMillis.toString(), "0", // during today, not all day
allDayStartInMillis.toString(), "1" // Started today at default start-time for all-day events (all-day), default time zone
};
可以将查询细化为不需要 2 个部分,但这对我来说已经足够了。
如果有帮助,这里是 dayStart 和 dayEnd 的来源:
Time dayStart = new Time();
dayStart.setToNow();
dayStart.hour=0;
dayStart.minute=0;
dayStart.second=0;
Time dayEnd = new Time();
dayEnd.set(dayStart);
dayEnd.hour=dayStart.hour+23;
dayEnd.minute=dayStart.minute+59;
dayEnd.second=dayStart.second+59;
Long dayStartInMillis = dayStart.toMillis(false);
Long dayEndInMillis = dayEnd.toMillis(false) + 999;
Long allDayStartInMillis = allDayStart.toMillis(false);