【问题标题】:How to query android calendar events for specific date range?如何查询特定日期范围的 android 日历事件?
【发布时间】:2013-03-06 20:24:49
【问题描述】:

我正在查询 android 中的事件表以获取事件。

String[] projection = new String[] { "calendar_id", "title", "description",
                    "dtstart", "dtend", "eventLocation" };

Cursor cursor = cr.query(Uri.parse("content://com.android.calendar/events"),
                    projection,
                    null,
                    null,
                    null);

这将返回所有事件。但现在我只想要特定持续时间的事件,比如 2013 年 4 月 3 日到 2013 年 7 月 3 日。如何使用 selectionselectionArgs[] ? 我试过这个

String selection = "((dtstart <= ?) AND (dtend >= ?))";
String[] selectionArgs = new String[] {startString, endString};

但它没有返回任何东西。我的疑问是 1. where 子句会起作用吗?因为, startDate 和 endDate 先转换为 long(毫秒),然后转换为 String,然后存储在表中。那么如何比较字符串的长值。 sqlite 中没有toNumber() 函数。 2.如果我传递selectionArgs,那么startStringendString应该是哪种格式?

【问题讨论】:

标签: android events calendar selection


【解决方案1】:

startString & endString 应该以毫秒为单位传递。尝试使用:

Calendar c_start= Calendar.getInstance();
c_start.set(2013,2,4,0,0); //Note that months start from 0 (January)   
Calendar c_end= Calendar.getInstance();
c_end.set(2013,2,7,0,0); //Note that months start from 0 (January)

另外,我认为你的逻辑是颠倒的,日期范围应该是这样的:

String selection = "((dtstart >= "+c_start.getTimeInMillis()+") AND (dtend <= "+c_end.getTimeInMillis()+"))";

【讨论】:

  • 这就是解决方案!
猜你喜欢
  • 1970-01-01
  • 2012-01-22
  • 2021-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多