Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting date content values on android calendar

Following is the code which i have used to add a event on Android Calendar

Cursor cursor=getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"), new String[]{"_id", "displayname"}, null, null, null);
cursor.moveToFirst();
// Get calendars name
String calendarNames[] = new String[cursor.getCount()];
// Get calendars id
int[] calendarId = new int[cursor.getCount()];
for (int i = 0; i < calendarNames.length; i++)
{
         calendarId[i] = cursor.getInt(0);
         calendarNames[i] = cursor.getString(1);
         cursor.moveToNext();
}

cursor.close(); 

ContentValues contentEvent = new ContentValues();
contentEvent.put("calendar_id", 1);
contentEvent.put("title", calTitle);                                                           
contentEvent.put("description", "Test Event");                                  
contentEvent.put("eventLocation", "New York");                            
long startTime = System.currentTimeMillis() + 1000 * 60 * 60;
long endTime = System.currentTimeMillis() + 1000 * 60 * 60 * 2;

contentEvent.put("dtstart", startTime);
contentEvent.put("dtend", endTime);                                        
contentEvent.put("allDay", 1);                                                                
contentEvent.put("hasAlarm",1);

Uri eventsUri = Uri.parse("content://com.android.calendar/events");    
getContentResolver().insert(eventsUri, contentEvent);

How do i change the dtstart and dtend to dates which I prefer? Is there a specific format for this? for example: say I need to add dtstart to 02/11/2013 and dtend to 03/11/2013

like image 504
Chamara Keragala Avatar asked Jan 06 '14 09:01

Chamara Keragala


1 Answers

Are you looking for something like this?

public long getLongAsDate(int year, int month, int date) {
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.DAY_OF_MONTH, date);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.YEAR, year);
        return calendar.getTimeInMillis();
}

Just call getLongAsDate(year, month, date) with the desired date, e.g. getLongAsDate(2013, 11, 2)

...
long startTime = getLongAsDate(2013, 11, 2);
long endTime = getLongAsDate(2013, 11, 3);
contentEvent.put("dtstart", startTime);
contentEvent.put("dtend", endTime);   
...
like image 195
Endzeit Avatar answered Sep 30 '22 14:09

Endzeit