- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 CalendarContract.Instances 获取一组日历事件。一般来说,我的查询工作正常。但是,“假期”日历中事件的开始和结束时间在错误的时区返回。我的一个个人日历中的事件都有正确的时间。
例如:
New Year's day "begins" at 04:00 PM, 31 Dec 2014.
在哪里
Opera "begins" at 02:00 PM, 11 Jan 2015.
我使用完全相同的代码来显示两者:
SimpleDateFormat formatter = new SimpleDateFormat ("hh:mm a, d MMM yyyy", Locale.US);
logD (prefix + i + ": " + formatter.format (data.startTime) + "; " + data.note);
其中 data.startTime 映射到 Instances.BEGIN,data.note 映射到 Instances.TITLE。歌剧在正确的时间放映,元旦显然是 8 小时(我在美国太平洋时区)。
如果我在 Android 日历应用程序中查看这些,两者都会显示正确的时间。
显然,我可以查看事件来自哪个日历并相应地设置时区以使其以正确的时间显示。但是,我希望有一个我不知道的更合适的解决方案。
这是从光标获取事件值的代码 fragment :
@Override
public View getView (int position, View convertView, ViewGroup parent)
{
...
EventFields fields = new EventFields();
cursor.moveToPosition (position);
fields.title = cursor.getString (cursor.getColumnIndex (Instances.TITLE));
fields.dtStart = cursor.getLong (cursor.getColumnIndex (Instances.BEGIN));
fields.dtEnd = cursor.getLong (cursor.getColumnIndex (Instances.END));
fields.iCalDuration = cursor.getString (cursor.getColumnIndex (Instances.DURATION));
fields.rrule = cursor.getString (cursor.getColumnIndex (Instances.RRULE));
...
}
这是查询:
@Override
public void refreshData (String constraint)
{
long begin = ... some date ...
long end = ... another date ...
final Uri uri = Uri.parse(CalendarContract.Instances.CONTENT_URI + "/" +
Long.toString(begin) + "/" +
Long.toString(end));
// Setup query - projection ordering must match statics above.
final String[] projection = new String[] {
Instances._ID,
Instances.EVENT_ID,
Instances.TITLE,
Instances.BEGIN,
Instances.END,
Instances.DURATION,
Instances.RRULE,
Instances.DESCRIPTION,
};
final String sortOrder = Instances.BEGIN;
String selection = null;
if (constraint != null)
selection = Instances.TITLE + " like '%" + constraint.toString() + "%'";
cursor = getActivity().getContentResolver().query (
uri,
projection,
selection,
null,
sortOrder);
}
如上例,元旦
New Year's day dtStart = 1419984000000 and
另一个真正在下午 4 点开始的 Activity 是
Roger dtStart = 1420052400000
最佳答案
正如评论中所讨论的,我们确定这是与 All Day 事件以 UTC 显示时间相关的问题。
看起来这是just how Android handles it .
查看 Format Time class in the Android Docs
public boolean allDay
True if this is an allDay event. The hour, minute, second fields are all zero, and the date is displayed the same in all time zones.
最后是 CalendarContract.Events docs
If allDay is set to 1 eventTimezone must be TIMEZONE_UTC and the time must correspond to a midnight boundary.
所以你所做的格式化全天事件是正确的
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
更新
默认情况下,如果您没有明确设置 SimpleDateFormat 的时区,它将默认为您设备的本地时区。
您的 BEGIN 和 END 时间应该以 UTC 时间返回。
全天事件始终设置为 UTC 时区的午夜,因此将它们格式化为 UTC 时区以外的任何时间都会为您提供非午夜时间。
您可以使用继承字段检查日历事件实例的时区:
EVENT_TIMEZONE
关于android - 使用 CalendarContract.Instances 查询 "all_day"事件导致时区错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27516398/
我知道我的问题有点含糊,但我不知道如何描述它。我问过很多地方,但似乎没有人理解我为什么要这样做。但请耐心等待,我会解释为什么我想要这样的东西。 我使用 Liquid Templates 允许用户在我的
这个问题在这里已经有了答案: what is the difference between null != object and object!=null [duplicate] (2 个回答) 7年
当我在我的本地主机 Google App Engine 应用程序中将日志记录级别更改为 FINE 时,我开始在我的跟踪堆栈中看到这些: Apr 17, 2013 4:54:20 PM com.goog
Python 有内置函数 type : class type(object) With one argument, return the type of an object. The return v
我正在使用深度学习进行语义分割,我遇到了以下术语:语义分割、实例检测、对象检测 和对象分割. 它们有什么区别? 最佳答案 这些术语的某些用法对用户而言是主观的或依赖于上下文,但据我所知对这些术语的合理
我面临 -[NSConcreteMutableData release] 的问题:消息发送到已释放的实例,我也附上了我的示例代码。 - (IBAction)uploadImage { NSString
我试图显示模型中的单个实例(数据库行),其中多个实例共享多行的相同字段(列)值。为了澄清这一说法,我有以下情况: ID/Title/Slug/Modified 1 Car A 1s ag
我正在尝试使用mockito来模拟服务。然而,我没有找到一种方法来告诉mockito,给定一个类的实例返回给我相同的实例: 类似于: given(service.add(any(Individua
我知道如何从父类(super class)原型(prototype)创建子类原型(prototype)。但是,如果我已经有了父类(super class)对象的实例来创建子类对象怎么办? 在 JS 中
鉴于 Kotlin 1.1。对于某个类的 instance,instance::class.java 和 instance.javaClass 似乎几乎是等价的: val i = 0 println(
这个问题在这里已经有了答案: 8年前关闭。 Possible Duplicate: Find out the instance id from within an ec2 machine 我正在寻找从
为什么我的 Instantiate 函数没有创建 That 的“空白”实例? 我有以下最小类: classdef That < handle properties This = '' end
Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTra
考虑以下几点: public class A { public String name = "i am a A instance"; } public class B extends A {
我正在使用 Scalr 来扩展网站服务器。 在 Apache 服务器上,我安装了 Sakai,并为 Linux 机器创建了一个启动脚本。 问题是,如何确保MySQL实例在Apache服务器启动之前启动
Android Realm DB 允许使用 Realm.getInstance() 获取多个实例。这些中的最佳实践是什么? :1.创建单个实例(应用程序范围)并在任何地方使用它2. 在需要时获取一个新
我很难理解为什么修改实例 a 中的属性会修改实例 b 中的相同属性。 var A = function (){ }; A.prototype.data = { value : 0 }; var
我将 Weka 用作更长管道的一部分,因此,我无法承受将所有数据写入文件或数据库只是为了创建一个 Instances。目的。我可以即时做的是创建 Instance 的列表对象。 来自 this pag
class C: def func(self, a): print(a) c = C() print(c.__dict__) # {} c.func = c.func # c.func i
Angular Routing 文档提到了组件实例创建、组件实例激活和路由激活。 文档没有解释这些概念的区别,以及每次创建/激活发生的时间。 问题 实例创建和实例激活有什么区别? 实例激活和路由激活有
我是一名优秀的程序员,十分优秀!