Android 中的 RRULE

发布于 2025-01-09 19:07:22 字数 1226 浏览 0 评论 0原文

我正在尝试在日历中设置每周星期一早上 9 点提醒 以下是代码

final int[] preTimings = {9, 12, 18};

Calendar calendar = Calendar.getInstance(Locale.getDefault());
ContentValues newEvent = new ContentValues();

int month = calendar.get(Calendar.MONTH);
int date = calendar.get(Calendar.DATE);

calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DATE, date);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

for(int i = 0; i < preTimings.length; i++{
    calendar.set(Calendar.HOUR_OF_DAY, preTimings[i]);
    long timeInMillis = calendar.getTimeInMillis();

    newEvent.put(CalendarContract.Events.CALENDAR_ID, 1);
    newEvent.put(CalendarContract.Events.TITLE, preTitle[i]);
    newEvent.put(CalendarContract.Events.DTSTART, timeInMillis);                   
    newEvent.put(CalendarContract.Events.DTEND, timeInMillis + 60000);
    newEvent.put(CalendarContract.Events.RRULE, "FREQ=WEEKLY;BYDAY=MO");
    newEvent.put(CalendarContract.Events.HAS_ALARM, true);
    newEvent.put(CalendarContract.Events.EVENT_TIMEZONE, "GMT-05:30");

因此,该程序在每周一正确设置中午 12 点和下午 6 点,但由于某种原因,它在每周周二设置上午 9 点。 我不知道这里出了什么问题。我什至尝试在在线计算器中转换 timeInMillis 以了解结果是什么,但它们是正确的。

I am trying to set weekly monday reminder in calendar at 9 a.m.
Following is the code

final int[] preTimings = {9, 12, 18};

Calendar calendar = Calendar.getInstance(Locale.getDefault());
ContentValues newEvent = new ContentValues();

int month = calendar.get(Calendar.MONTH);
int date = calendar.get(Calendar.DATE);

calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DATE, date);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

for(int i = 0; i < preTimings.length; i++{
    calendar.set(Calendar.HOUR_OF_DAY, preTimings[i]);
    long timeInMillis = calendar.getTimeInMillis();

    newEvent.put(CalendarContract.Events.CALENDAR_ID, 1);
    newEvent.put(CalendarContract.Events.TITLE, preTitle[i]);
    newEvent.put(CalendarContract.Events.DTSTART, timeInMillis);                   
    newEvent.put(CalendarContract.Events.DTEND, timeInMillis + 60000);
    newEvent.put(CalendarContract.Events.RRULE, "FREQ=WEEKLY;BYDAY=MO");
    newEvent.put(CalendarContract.Events.HAS_ALARM, true);
    newEvent.put(CalendarContract.Events.EVENT_TIMEZONE, "GMT-05:30");

So this program is setting the 12 p.m and 6 p.m correctly on Mondays every week but for some reason it is setting the 9 a.m on Tuesdays every week.
I don't know what is wrong over here. I even tried converting the timeInMillis in an online calculator to know what is the result, but they are correct.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攒眉千度 2025-01-16 19:07:22

所以问题出在最后一行,尽管我已经明确声明了时区,但它的设置不正确。

因此,

newEvent.put(CalendarContract.Events.EVENT_TIMEZONE, "GMT-05:30");

它应该是

newEvent.put(CalendarContract.Events.EVENT_TIMEZONE, String.valueOf(TimeZone.getTimeZone("UTC")));

仍然不知道为什么它在上午 11 点及以上时间有效。有点奇怪

So the issue was on the last line, even though i have specifically declared the timezone, it was setting it incorrectly.

So instead of

newEvent.put(CalendarContract.Events.EVENT_TIMEZONE, "GMT-05:30");

It should be

newEvent.put(CalendarContract.Events.EVENT_TIMEZONE, String.valueOf(TimeZone.getTimeZone("UTC")));

Still no idea why it worked for times 11 a.m and above. Kinda weird

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文