如何查询特定日期范围的android日历事件?

问题描述:

我在android中查询事件表以获取事件。如何查询特定日期范围的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年3月7日的特定时间内举办活动。我如何使用selectionselectionArgs[]? 我想这

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

但它不返回任何东西。我的疑问是 1. Where子句是否工作?因为,startDate和endDate首先转换为长(毫秒),然后转换为字符串,然后存储在表中。那么如何比较字符串的长期价值。 sqlite中没有toNumber()函数。 2.如果我通过selectionArgs那么startStringendString应该采用哪种格式?

+0

检查:http://stackoverflow.com/questions/26844770/how-to-获取日历上的Android手机 – 2014-11-11 09:13:12

startString & endString应该以millis方式通过。尝试使用:

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()+"))"; 
+0

你能否详细解答这个答案... – Deepak 2013-10-30 12:30:06

+0

这是解决方案! – 2014-11-11 08:37:04