如何在Android中按日期范围或月份获取日历事件 [英] How to get Calendar event by date range or month in android

查看:230
本文介绍了如何在Android中按日期范围或月份获取日历事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在我的应用程序中获取和加载日历事件.运行良好.但是现在我想获取指定日期范围内的事件.我如何获得它.

I am using below code to get and load calendar events in my application. It's working perfectly. But now i want to get events specified date range. How can i get it..

Cursor cursor = getContentResolver().query(
                Uri.parse("content://com.android.calendar/events"),
                new String[] { "_id", "title", "dtstart", "dtend" }, null,
                null, "dtstart ASC");

应该是
dtstart ="2013-01-01"
dtend ="2013-01-31"

should be
dtstart = "2013-01-01"
dtend= "2013-01-31"

推荐答案

这是我为检索事件而实现的:

This was what I implemented to retrieve the events:

if (Integer.parseInt(Build.VERSION.SDK) >= 8 || Integer.parseInt(Build.VERSION.SDK) <= 13 ) {

            uri = "content://com.android.calendar/events";
            CALENDAR_URI = Uri.parse(uri);

        } else if(Integer.parseInt(Build.VERSION.SDK) >= 14){

             CALENDAR_URI = CalendarContract.Events.CONTENT_URI;

        }
        else {

            uri = "content://calendar/events";
            CALENDAR_URI = Uri.parse(uri);

        }
Cursor cursors = context.getContentResolver().query(CALENDAR_URI, new String[]{ "_id", "title", "description", "dtstart", "dtend", "eventLocation" },
                null,null, null);



 cursors.moveToFirst();
                                    String[] CalNames = new String[cursors.getCount()];
                                    int[] CalIds = new int[cursors.getCount()];
                                    for (int i = 0; i < CalNames.length; i++) {
                                        CalIds[i] = cursors.getInt(0);
                                        CalNames[i] = "Event"+cursors.getInt(0)+": \nTitle: "+ cursors.getString(1)+"\nDescription: "+cursors.getString(2)+"\nStart Date: "+new Date(cursors.getLong(3))+"\nEnd Date : "+new Date(cursors.getLong(4))+"\nLocation : "+cursors.getString(5);   

                                        Date mDate = new Date(cursors.getLong(3));
                                        Date nDate = new Date(cursors.getLong(4));

                                        long mTime = mDate.getTime();
                                        long lTime = nDate.getTime();
                                        if(stTime <= mTime && enTime >= lTime){ 
 String eid = cursors.getString(0);

                                            int eID = Integer.parseInt(eid);

                                            String desc = cursors.getString(2);
                                            String title = cursors.getString(1);

在这种情况下,您的stTime将是dtstart,enTime将是您的dtend.

In this case your stTime will be yo dtstart and enTime will be your dtend.

这篇关于如何在Android中按日期范围或月份获取日历事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆