在android日历上设置日期内容值
作者:互联网
以下是我用于在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日并投入到2013年11月3日
解决方法:
你在找这样的东西吗?
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);
...
标签:android-calendar,android,android-intent,calendar 来源: https://codeday.me/bug/20190725/1531952.html