2

I am facing an issue while adding an event to the Google Calendar as its showing an wrong end date in the Google Calendar Screen But if you add the event it will generate an event with proper end date. Not sure why its showing the 1 day before an end date.

For e.g , if end date is 31-Aug it will displayed on the calendar preview screen as 30-Aug but event is generated for 31-Aug Only .

List<String> dateFormats = Arrays.asList(CALENDER_EVENT_DATE_FORMAT_1,
                CALENDER_EVENT_DATE_FORMAT_2);

        Calendar calStartDate = Calendar.getInstance();
        Calendar calEndDate = Calendar.getInstance();
        Date eventStartDate = null;
        Date eventEndDate = null;
        for (String format : dateFormats) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            try {
                eventStartDate = sdf.parse(startDate);
                eventEndDate = sdf.parse(endDate);
            } catch (ParseException e) {

            }
        }

        Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setType(CALENDER_EVENT_TYPE);
        intent.putExtra(CALENDER_EVENT_TITLE, title);
        intent.putExtra(CALENDER_EVENT_DESC, note);
        intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, isAllDay);

        calStartDate.set(Calendar.HOUR_OF_DAY, 0);            // set hour to midnight
        calStartDate.set(Calendar.MINUTE, 0);                 // set minute in hour
        calStartDate.set(Calendar.SECOND, 0);                 // set second in minute
        calStartDate.set(Calendar.MILLISECOND, 0);            // set millisecond in second
        calStartDate.setTime(eventStartDate);

        calEndDate.set(Calendar.HOUR_OF_DAY, 0);            // set hour to midnight
        calEndDate.set(Calendar.MINUTE, 0);                 // set minute in hour
        calEndDate.set(Calendar.SECOND, 0);                 // set second in minute
        calEndDate.set(Calendar.MILLISECOND, 0);            // set millisecond in second
        calEndDate.setTime(eventEndDate);

        intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
                calStartDate.getTimeInMillis());
        intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
                calEndDate.getTimeInMillis());

        weakActivity.get().startActivity(intent);
halfer
  • 19,824
  • 17
  • 99
  • 186
Born To Win
  • 3,319
  • 3
  • 19
  • 27

1 Answers1

0

This may help:

reference

Writing to Events

If allDay is set to 1 eventTimezone must be Time.TIMEZONE_UTC and the time must correspond to a midnight boundary.

Stack overflow