How to get all the events from calendar?

14,641

Try this code...

public class Utility {

    public static ArrayList<String> nameOfEvent = new ArrayList<String>();
    public static ArrayList<String> startDates = new ArrayList<String>();
    public static ArrayList<String> endDates = new ArrayList<String>();
    public static ArrayList<String> descriptions = new ArrayList<String>();

    public static ArrayList<String> readCalendarEvent(Context context) {
        Cursor cursor = context.getContentResolver()
                .query(
                        Uri.parse("content://com.android.calendar/events"),
                        new String[] { "calendar_id", "title", "description",
                                "dtstart", "dtend", "eventLocation" }, null,
                        null, null);
        cursor.moveToFirst();
        // fetching calendars name
        String CNames[] = new String[cursor.getCount()];

        // fetching calendars id
        nameOfEvent.clear();
        startDates.clear();
        endDates.clear();
        descriptions.clear();
        for (int i = 0; i < CNames.length; i++) {

            nameOfEvent.add(cursor.getString(1));
            startDates.add(getDate(Long.parseLong(cursor.getString(3))));
            endDates.add(getDate(Long.parseLong(cursor.getString(4))));
            descriptions.add(cursor.getString(2));
            CNames[i] = cursor.getString(1);
            cursor.moveToNext();

        }
        return nameOfEvent;
    }

    public static String getDate(long milliSeconds) {
        SimpleDateFormat formatter = new SimpleDateFormat(
                "dd/MM/yyyy hh:mm:ss a");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(milliSeconds);
        return formatter.format(calendar.getTime());
    }
}

You can also check this

Getting events from calendar

Share:
14,641
Jignesh Ansodariya
Author by

Jignesh Ansodariya

Ansodariya Jignesh J. Android Developer.

Updated on June 29, 2022

Comments

  • Jignesh Ansodariya
    Jignesh Ansodariya almost 2 years

    Hi I am able to get events using this code

    cur = null;
        cr = getContentResolver();
    
    
        // Construct the query with the desired date range.
        Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
    
        ContentUris.appendId(builder, 0);
        ContentUris.appendId(builder, endMillis);
    
    
        // Submit the query
        cur =  cr.query(builder.build(), 
            INSTANCE_PROJECTION, 
            null, 
            null, 
            null);
    

    but as you see I can get only events within the given time duration, but I need all the events (no any time specification excluding repeated events) is this possible ? and how ? please help me.

    cur =  cr.query(Uri.parse("content://com.android.calendar/events"), 
            INSTANCE_PROJECTION, 
            null, 
            null, 
            null);
    

    it gives the error java.lang.IllegalArgumentException:

  • Amit Hooda
    Amit Hooda over 11 years
  • Adevelopment
    Adevelopment over 6 years
    How to get Listofattendees using Calendar