Adding events date and time in android calendar

2019-07-17 01:20发布

问题:

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, 02:10PM

Following is my code to add to events

                    long startTime = 0, endTime = 0;
                    Date d = new Date();
            startTime = d.parse(start);
            d.setTime(startTime);

            endTime = d.parse(end);
            d.setTime(endTime);

            Intent intent = new Intent(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, startTime);
            intent.putExtra(Events.DTEND, endTime);
            intent.putExtra(Events.ALL_DAY, allDayFlag);
            intent.putExtra(Events.STATUS, 1);
            intent.putExtra(Events.VISIBLE, 0);
            intent.putExtra(Events.HAS_ALARM, 1);
            startActivity(intent);

But in the Event details page i am getting the title, description alone to be in correct. In the place of start and end date i am getting the current date where as the start and end time to be of next 1 hr time.

Can anyone say me whats going wrong in my code.....

回答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);


回答2:

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

public static String getCalendarUriBase() {

    String calendarUriBase = null;
    Uri calendars = Uri.parse("content://calendar/calendars");
    Cursor managedCursor = 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() {

    DateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy, h:mma");
    String dateStarts = "May 05, 2012, 07:10PM";
    String dateEnds = "May 06, 2012, 02:10PM";
    Uri EVENTS_URI = Uri.parse(getCalendarUriBase() + "events");
    ContentResolver cr = getActivity().getContentResolver();
    // event insert
    ContentValues values = new ContentValues();
    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);// now
    try {
        cr.insert(EVENTS_URI, values);
        // reminder insert
        Uri REMINDERS_URI = Uri.parse(getCalendarUriBase()
                + "reminders");
        values = new ContentValues();
        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



回答3:

It might be android Sdk problem, please Try this one

    int SDK_INT = android.os.Build.VERSION.SDK_INT;
    DateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy, h:mmaa");
    long lnsTime = 0, lneTime = 0;

    Date dateObject;

    try {
        String dob_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());
    }

    Intent intent = new Intent(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



回答4:

I think may be your date parsing cause this problem.

Try this

DateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy, h:mmaa"); 
long lnsTime, lneTime;

        Date dateObject;

        try{
        String dob_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());
            }

     Intent intent = new Intent(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...