问题描述
我使用官方网站上的教程编写了一个简单的代码,将全天事件插入日历.http://developer.android.com/guide/topics/providers/calendar-provider.html
I wrote a simple code to insert an all day event into the calendar, using the tutorial from the official site.http://developer.android.com/guide/topics/providers/calendar-provider.html
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, dueDate.getTime());
values.put(Events.ALL_DAY, true);
values.put(Events.DTEND, dueDate.getTime());
values.put(Events.TITLE, "Some Event");
values.put(Events.CALENDAR_ID, mCalID);
TimeZone tz = TimeZone.getDefault();
values.put(Events.EVENT_TIMEZONE, tz.getID());
Uri uri = cr.insert(Events.CONTENT_URI, values);
我发现当我在运行 4.03 的 Acer 设备上打开 Google 日历应用程序时,输入的日期已向后移动 1 天.我的本地时区是悉尼,即 GMT+10.
I found that when I opened the Google Calendar app on the my Acer device running 4.03, the date entered had been shifted back by 1 day. My local timezone is Sydney which is GMT+10.
所以我去设置将本地时区更改为美国东部 (GMT -5) 并运行相同的代码,并且日期没有变化.然后我将时区更改为 GMT+2,伊斯坦布尔,日期发生了变化.然后我改到伦敦格林威治标准时间 0,没有班次.
So I went to settings changed the local time zone to American Eastern (GMT -5) and ran the same code and there was no shift in the dates. Then I changed the timezone to GMT+2, Istanbul and there was a shift in the dates. Then I changed to London GMT 0, and there was no shift.
当我进行非全天活动时,无论时区如何,都会在日历中输入正确的时间.
When I did non all day events the correct times were entered into the calendar regardless of the timezone.
我发现的最接近的错误报告是这个http://code.google.com/p/android/issues/detail?id=14051
The closest bug report I found was thishttp://code.google.com/p/android/issues/detail?id=14051
我是否在代码中遗漏了什么,或者其他人也遇到过这种情况.
Am I missing something in the code, or have others also encountered this.
编辑使用内容解析器读取事件数据时的进一步检查
EditOn further inspection when reading Event data using content resolver
ContentResolver cr = context.getContentResolver();
Uri uri = CalendarContract.Events.CONTENT_URI;
String selection =CalendarContract.Events._ID + "=?";
String [] selectionArgs = new String[]{String.valueOf(eventID)};
转换为日期时的 long 值是正确的日期和时间,例如.2013 年 2 月 14 日 12.00 AEST(悉尼时间).所以问题一定出在谷歌日历读取这些值的方式上.当我从 Android 中的日历应用程序手动添加全天事件并使用内容解析器读取时间时,它是 AEST(悉尼时间)的日期和上午 11 点,对应于悉尼的 +11 GMT 关闭时间.因此,需要在 GMT 中输入全天事件以避免这种情况,但文档中没有提及这一点.
The value of the long when convert to date was the correct date and time eg. 14th Feb 2013 12.00 AEST (Sydney Time). So the problem must be in the way Google Calendar is reading in these values. When I added an all day event manually from the Calendar application in Android, and the read the time using content resolver it was the date and 11.00 am in AEST (Sydney Time), which corresponds to the +11 GMT off set in Sydney.So all day events need to be entered in GMT to avoid this, but there is no mention of this in the documentation.
所以现在我正在转移时间,以避免这个问题.
So now I am shifting the time, to avoid this problem.
推荐答案
几个想法:
- 全天事件的时区独立性"(视为具有 GMT)[1] [2]
- 国际日期变更线
[1]"如果 allDay 设置为 1,则 eventTimezone 必须为 TIMEZONE_UTC,并且时间必须对应于午夜边界."
[1]"If allDay is set to 1 eventTimezone must be TIMEZONE_UTC and the time must correspond to a midnight boundary."
[2]http://developer.android.com/reference/android/provider/CalendarContract.Events.html
这篇关于对于 GMT +x 区域,Android 日历全天活动日期提前一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!