【问题标题】:Why is Timezone offset added to Time undesirably (03:00 to 08:00)为什么不希望将时区偏移添加到时间(03:00 到 08:00)
【发布时间】:2016-04-28 06:35:55
【问题描述】:

在 Android 应用程序中,我将事件添加到用户的日历中。

我的问题是,我保存的时间发生了变化,当它存储在 Google 日历中时(意味着 +0500 的 03:00 到 08:00 偏移添加到我的时区 +0500 的时间)

请指导我做错了什么..

我正在使用此代码来设置日历活动的时间和日期

calendar.set(year, monthOfYear, dayOfMonth);

calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DateTime startDateTime = new DateTime(sdf.format(calendar.getTime()));
EventDateTime start = new EventDateTime().setDateTime(startDateTime);

event.setStart(start);

详细参考原代码: https://developers.google.com/google-apps/calendar/create-events

【问题讨论】:

    标签: android datetime google-calendar-api simpledateformat


    【解决方案1】:

    问题可能与您的SimpleDateFormat 有关,可以简化为:

    calendar.set(year, monthOfYear, dayOfMonth);
    
    calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calendar.set(Calendar.MINUTE, minute);
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    
    DateTime startDateTime = new DateTime(calendar.getTime())
    EventDateTime start = new EventDateTime().setDateTime(startDateTime);
    
    event.setStart(start);
    

    Calendar 暴露了底层的Date,而DateTime 有一个接受Date 的构造函数,因此不需要解析它。

    【讨论】:

    • 先生,更新后的行给出了以下错误.. EventDateTime 中的 setDateTime(com.google.api.client.util.DateTime) 无法应用于 (java.util.Date)
    • @AL 我很抱歉。我混淆了DateDateTime。请查看我的更新答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    相关资源
    最近更新 更多