以下是我用于在Android日历上添加活动的代码
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);
如何将dtstart
和dtend
更改为我更喜欢的日期?这有特定的格式吗?例如:说我需要将dtstart
添加到2013年11月2日,将dtend
添加到2013年11月3日
答案 0 :(得分:9)
你在找这样的东西吗?
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();
}
只需使用所需日期调用getLongAsDate(年,月,日),例如getLongAsDate(2013,11,2)
...
long startTime = getLongAsDate(2013, 11, 2);
long endTime = getLongAsDate(2013, 11, 3);
contentEvent.put("dtstart", startTime);
contentEvent.put("dtend", endTime);
...
答案 1 :(得分:0)
尝试这样的事情
Calendar cl = Calendar.getInstance();
cl.setTimeInMillis(milliseconds); //here your time in miliseconds
String date = "" + cl.get(Calendar.DAY_OF_MONTH) + "/" + cl.get(Calendar.MONTH) + "/" + cl.get(Calendar.YEAR);