Skip to content Skip to sidebar Skip to footer

Adding Events Date And Time In Android Calendar

I am trying to add the following date and time in my android calendars event.... Title and description = Events for test Start Date = May 05, 2012, 07:10PM End Date = May 06, 2012

Solution 1:

if your are getting the data as follows Date = May 05, 2012, 07:10PM, then convert the date to be in number format a May to be 05 and pass the value in the following code.

Calendar beginCal = Calendar.getInstance();
                beginCal.set(year, mnth, day, hrs, min);
                startTime = beginCal.getTimeInMillis();

                Calendar endCal = Calendar.getInstance();
                endCal.set(year, mnth, day, hrs, min);
                endTime = endCal.getTimeInMillis();     

                Intent intent =new Intent(Intent.ACTION_INSERT);
                intent.setType("vnd.android.cursor.item/event");
                intent.putExtra(Events.TITLE, summary);
                intent.putExtra(Events.DESCRIPTION, summary);
                intent.putExtra(Events.EVENT_LOCATION, "");     
                intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginCal.getTimeInMillis());
                intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endCal.getTimeInMillis());
                intent.putExtra(Events.ALL_DAY, allDayFlag);
                intent.putExtra(Events.STATUS, 1);
                intent.putExtra(Events.VISIBLE, 0);
                intent.putExtra(Events.HAS_ALARM, 1);
                startActivity(intent);

Solution 2:

Instead of passing intents, we can also add events to calendar like this, which looks pretty cool

public static String getCalendarUriBase() {

StringcalendarUriBase=null;
    Uricalendars= Uri.parse("content://calendar/calendars");
    CursormanagedCursor=null;
    try {
        managedCursor = act.managedQuery(calendars, null, null, null, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (managedCursor != null) {
        calendarUriBase = "content://calendar/";
    } else {
        calendars = Uri.parse("content://com.android.calendar/calendars");
        try {
            managedCursor = act.managedQuery(calendars, null, null, null,
                    null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (managedCursor != null) {
            calendarUriBase = "content://com.android.calendar/";
        }
    }
    // //Log.d("Calendar", "" + calendarUriBase);return calendarUriBase;
}

main() {

    DateFormatformatter=newSimpleDateFormat("MMMM dd, yyyy, h:mma");
    StringdateStarts="May 05, 2012, 07:10PM";
    StringdateEnds="May 06, 2012, 02:10PM";
    UriEVENTS_URI= Uri.parse(getCalendarUriBase() + "events");
    ContentResolvercr= getActivity().getContentResolver();
    // event insertContentValuesvalues=newContentValues();
    Date startDate, endDate;
    startDate = formatter.parse(dateStarts);
    endDate = formatter.parse(dateEnds);
    values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
    values.put("calendar_id", 1);
    values.put("title", remainderTitle);
    values.put("allDay", 0);
    values.put("dtstart", startDate.getTime());
    values.put("dtend", enddDate.getTime());
    values.put("description", remainderInfo);
    values.put("visibility", 0);
    values.put("hasAlarm", 1);// nowtry {
        cr.insert(EVENTS_URI, values);
        // reminder insertUriREMINDERS_URI= Uri.parse(getCalendarUriBase()
                + "reminders");
        values = newContentValues();
        values.put("event_id", 1);
        values.put("method", 1);
        values.put("minutes", 10);
        cr.insert(REMINDERS_URI, values);

    } catch (Exception e) {
        e.printStackTrace();
    }

}

Hope, it will definitely help's you :)

Thanks

Solution 3:

It might be android Sdk problem, please Try this one

intSDK_INT= android.os.Build.VERSION.SDK_INT;
    DateFormatformatter=newSimpleDateFormat("MMMM dd, yyyy, h:mmaa");
    longlnsTime=0, lneTime = 0;

    Date dateObject;

    try {
        Stringdob_var="May 05, 2012, 07:10PM";

        dateObject = formatter.parse(dob_var);

        lnsTime = dateObject.getTime();
        Log.e(null, Long.toString(lnsTime));

        dob_var = "May 06, 2012, 02:10PM";

        dateObject = formatter.parse(dob_var);

        lneTime = dateObject.getTime();
        Log.e(null, Long.toString(lneTime));
    }

    catch (java.text.ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.i("E11111111111", e.toString());
    }

    Intentintent=newIntent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    if (SDK_INT >= 15) {
        intent.putExtra(Events.CALENDAR_ID, 1);
        intent.putExtra(Events.TITLE, "Title");
        intent.putExtra(Events.DESCRIPTION, "Title");
        intent.putExtra(Events.DTSTART, lnsTime);
        intent.putExtra(Events.DTEND, lneTime);
        intent.putExtra(Events.ALL_DAY, 0);
        intent.putExtra(Events.STATUS, 1);
        intent.putExtra(Events.VISIBLE, 0);
        intent.putExtra(Events.HAS_ALARM, 1);
    } else {
        intent.putExtra("calendar_id", 1);
        intent.putExtra("status", 1);
        intent.putExtra("visibility", 0);
        intent.putExtra("hasAlarm", 1);
        intent.putExtra("beginTime", lnsTime);
        intent.putExtra("allDay", true);
        intent.putExtra("rrule", "FREQ=YEARLY");
        intent.putExtra("endTime", lneTime);
        intent.putExtra("title", "Title");
        intent.putExtra("description", "Title");
    }
    startActivity(intent);

Hope it helps you :)

Thanks

Solution 4:

I think may be your date parsing cause this problem.

Try this

DateFormatformatter=newSimpleDateFormat("MMMM dd, yyyy, h:mmaa"); 
long lnsTime, lneTime;

        Date dateObject;

        try{
        Stringdob_var="May 05, 2012, 07:10PM";

        dateObject = formatter.parse(dob_var);

        lnsTime = dateObject.getTime();
        Log.e(null, Long.toString(lnsTime));

        dob_var = "May 06, 2012, 02:10PM";      

        dateObject = formatter.parse(dob_var);

        lneTime = dateObject.getTime();
        Log.e(null, Long.toString(lneTime));
        }

        catch (java.text.ParseException e) 
            {
            // TODO Auto-generated catch block
                e.printStackTrace();
                Log.i("E11111111111", e.toString());
            }

     Intentintent=newIntent(Intent.ACTION_EDIT);
            intent.setType("vnd.android.cursor.item/event");
            intent.putExtra(Events.TITLE, summary);
            intent.putExtra(Events.DESCRIPTION, summary);
            intent.putExtra(Events.EVENT_LOCATION, "");
            intent.putExtra(Events.DTSTART, lnsTime);
            intent.putExtra(Events.DTEND, lneTime);
            intent.putExtra(Events.ALL_DAY, allDayFlag);
            intent.putExtra(Events.STATUS, 1);
            intent.putExtra(Events.VISIBLE, 0);
            intent.putExtra(Events.HAS_ALARM, 1);
            startActivity(intent);

Hope this will help you.

Thanks...

Post a Comment for "Adding Events Date And Time In Android Calendar"