问题
i am inserting events in my android calendar. the code is following:
ContentValues event = new ContentValues();
event.put("calendar_id", calId);
event.put("title", "Event Title");
event.put("description", "Event Desc");
event.put("eventLocation", "Event Location");
event.put("allDay", 1);
event.put("eventStatus", 1);
event.put("visibility", 0);
event.put("transparency", 0);
event.put("hasAlarm", 1);
Date d = new Date();
d.setHours(8);
d.setMinutes(30);
d.setSeconds(30);
long startTime = d.getTime();
d.setHours(12);
d.setMinutes(30);
d.setSeconds(20);
long endTime = d.getTime();
event.put("dtstart", startTime);
// event.put("dtend", endTime);
event.put("rrule", "FREQ=DAILY;WKST=SU");
// event.put("lastDate", endTime);
// event.put("timezone", "Asia/Karachi");
//event.put("duration", "P3600S");
//Calendar gmtC = new GregorianCalendar(TimeZone.getTimeZone("Asia/Karachi"));
// event.put("transparency", 0);
// event.put("hasAlarm", 1); // 0 for false, 1 for true
Uri eventsUri = Uri.parse("content://calendar/events");
Uri url = getContentResolver().insert(eventsUri, event);
i am getting the following exception:
java.lang.IllegalArgumentException: allDay is true but sec, min, hour are not 0.
need help!
回答1:
I ran into the same problem, even with hours, minutes and seconds set to 0. Then I discovered, that for allday events, the times have to be set in UTC. This means, you need to add the UTC offset of the calendar's timezone to your allday event start time.
For example: (timezone and startTime are only hardcoded for simplification purposes!)
// You should write a method to get the calendar's timezone through a query
String calendarTimezone = "CET";
// Start time of the event. Hours, minutes and seconds have to be 0.
long startTime = 1315087200000L; // equals 2011-09-04 00:00:00
// Get UTC offset for the given timezone and start time. This way DST is accounted for.
int timeZoneOffset = TimeZone.getTimeZone(calendarTimezone).getOffset(startTime);
// Set same time for start and end of the allday event. Add UTC offset.
event.put("dtstart", startTime + timeZoneOffset);
event.put("dtend", startTime + timeZoneOffset);
event.put("allDay", 1);
回答2:
The calendar GData API defines an all day event with a start time of just the date, and the end time of the day after the end of the event.
This is the data sent to google data api
<entry xmlns='http://www.w3.org/2005/Atom'
xmlns:gd='http://schemas.google.com/g/2005'>
<category scheme='http://schemas.google.com/g/2005#kind'
term='http://schemas.google.com/g/2005#event'></category>
<title type='text'>Word of the Day</title>
<gd:when startTime='2007-07-17'
endTime='2007-07-18'></gd:when>
</entry>
Notice the start/end times do not contain any time information in them.
You CANNOT have an all day event that doesn't start at midnight. That is why you are getting the exception. hour, min, sec MUST be 0 for them on an all day event.
You can try another forum, but you will always get this answer because this is how the GData API works.
回答3:
If allDay is set to 1 eventTimezone must be TIMEZONE_UTC and the time must correspond to a midnight boundary, means hour , minute , second should be zero of calendar object.
refer following link..
http://developer.android.com/reference/android/provider/CalendarContract.Events.html
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
cal.set(2014, 03, 18, 0, 0,0);
回答4:
you specify "allDay" as true, but you set a time with sec, min and hour. Which means for the system, taht it's not all day long... try removing either allDay or the time setting. Maybe these are opposite and contradictory.
回答5:
rob is correct! You have to define allday events in UTC. The following code is a little bit nicer than robs version:
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
long dtstart = cal.getTimeInMillis();
builder.withValue(Events.DTSTART, dtstart);
来源:https://stackoverflow.com/questions/3440172/getting-exception-when-inserting-events-in-android-calendar