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

问题描述 投票:0回答:1

我有一个按钮可以将事件添加到日历中。代码有效,事件添加正确,但是当显示要保存的日历事件时,时间显示不正确。活动时间是下午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

知道如何让它显示正确的时间吗?

UPDATE

没关系,问题出在我从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;
}
android xamarin.android calendar android-calendar
1个回答
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;
}
© www.soinside.com 2019 - 2024. All rights reserved.