将事件添加到日历会显示错误的时间,但是保存时间正确

时间:2019-04-10 16:53:58

标签: android xamarin.android calendar android-calendar

我有一个按钮可以向日历添加事件。代码有效,该事件已正确添加,但是当日历事件显示要保存时,时间显示不正确。该事件为下午4点至下午6点,日历添加事件显示为下午12点至下午2点,这显然是一个时区问题,但是保存事件(不更改时间)后,它会正确显示下午4点至下午6点。

这是添加事件和结果的代码和屏幕截图:

Intent eventValues = new Intent(Intent.ActionInsert);
eventValues.SetData(CalendarContract.Events.ContentUri);
eventValues.SetType("vnd.android.cursor.item/event");

eventValues.PutExtra(CalendarContract.Events.InterfaceConsts.Title, "Event Title");
eventValues.PutExtra(CalendarContract.Events.InterfaceConsts.Description, "Event Description");
eventValues.PutExtra(CalendarContract.ExtraEventBeginTime, GetDateTimeMS(2019, 3, 12, 16, 0));
eventValues.PutExtra(CalendarContract.ExtraEventEndTime, GetDateTimeMS(2019, 3, 12, 18, 0));
eventValues.PutExtra(CalendarContract.Events.InterfaceConsts.EventTimezone, "America/New_York");
eventValues.PutExtra(CalendarContract.Events.InterfaceConsts.EventEndTimezone, "America/New_York");
eventValues.PutExtra(CalendarContract.Events.InterfaceConsts.EventLocation, "Event Location");

StartActivity(eventValues);

添加事件: enter image description here

结果: enter image description here

有什么主意我可以让它显示正确的时间吗?

更新

不要紧记,问题出在我从Xamarin Docs获得的GetDateTimeMS函数中。我不得不将时区从默认更改为实际的TZ:

long GetDateTimeMS(int yr, int month, int day, int hr, int min)
{
    //Calendar c = Calendar.GetInstance(Java.Util.TimeZone.Default);
    Calendar c = Calendar.GetInstance(Java.Util.TimeZone.GetTimeZone("America/New_York"));

    c.Set(Java.Util.CalendarField.DayOfMonth, day);
    c.Set(Java.Util.CalendarField.HourOfDay, hr);
    c.Set(Java.Util.CalendarField.Minute, min);
    c.Set(Java.Util.CalendarField.Month, month);
    c.Set(Java.Util.CalendarField.Year, yr);

    return c.TimeInMillis;
}

1 个答案:

答案 0 :(得分:0)

问题出在我从Xamarin Docs获得的GetDateTimeMS函数中。我不得不将时区从默认更改为实际的TZ:

long GetDateTimeMS(int yr, int month, int day, int hr, int min)
{
    //Calendar c = Calendar.GetInstance(Java.Util.TimeZone.Default);
    Calendar c = Calendar.GetInstance(Java.Util.TimeZone.GetTimeZone("America/New_York"));

    c.Set(Java.Util.CalendarField.DayOfMonth, day);
    c.Set(Java.Util.CalendarField.HourOfDay, hr);
    c.Set(Java.Util.CalendarField.Minute, min);
    c.Set(Java.Util.CalendarField.Month, month);
    c.Set(Java.Util.CalendarField.Year, yr);

    return c.TimeInMillis;
}