Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to create an all-day event using Google Calendar API v3 for Java causes an error

I can create a timed event using the Java v3 Google Calendar API (as per the sample code on Google's website), but I need to create an all-day event.

I call the event's setStart() and setEnd(), i.e.

    event.setStart(startEventDateTime);
    event.setEnd(endEventDateTime);

These methods require and EventDateTime, i.e.

    EventDateTime startEventDateTime = new EventDateTime().setDateTime(startDateTime);
    EventDateTime endEventDateTime = new EventDateTime().setDateTime(endDateTime);

I use the setDateTime() methods as setDate() causes a 404 error.

setDateTime() requires a com.google.api.client.util.DateTime object, by doing

    DateTime startDateTime = new DateTime(startDate, TimeZone.getTimeZone("UTC"));
    DateTime endDateTime = new DateTime(endDate, TimeZone.getTimeZone("UTC"));

Passing in the TimeZone gives a time element so it's not an all day event.

I've tried setting dateOnly to true but this gives an error:

    DateTime startDateTime = new DateTime(true, startDate.getTime(), 0); 

I can't get the other ways of creating DateTime to work: Date date, TimeZone zone long value Date value long value, Integer tzShift String value

Which way do I create DateTime and can I use setDate(), i.e. new EventDateTime().setDate(...)?

Does anyone have a tested code snippet? Why isn't this documented by Google?

ps Interestingly, when reading events from Google, using getDate() causes an exception with timed events and getDateTime() an exception with all-day events. Need to use getDate() for all-day events and getDateTime() for timed events.

like image 525
user1825866 Avatar asked May 04 '13 08:05

user1825866


People also ask

Why I Cannot create an event in Google Calendar?

This usually indicates you do not have permissions to edit or add events to the calendar you picked. To fix this, ask the calendar owner to give you editing privileges on Google Calendar, and then reconnect your Google Calendar account in Zapier.


1 Answers

Fixed.

To create an all-day event, you must use setDate() having created DateTime objects using a String (which I created by formatting my Date objects). The code:

    Date startDate = new Date(); // Or a date from the database
    Date endDate = new Date(startDate.getTime() + 86400000); // An all-day event is 1 day (or 86400000 ms) long

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    String startDateStr = dateFormat.format(startDate);
    String endDateStr = dateFormat.format(endDate);

    // Out of the 6 methods for creating a DateTime object with no time element, only the String version works
    DateTime startDateTime = new DateTime(startDateStr);
    DateTime endDateTime = new DateTime(endDateStr);

    // Must use the setDate() method for an all-day event (setDateTime() is used for timed events)
    EventDateTime startEventDateTime = new EventDateTime().setDate(startDateTime);
    EventDateTime endEventDateTime = new EventDateTime().setDate(endDateTime);

    event.setStart(startEventDateTime);
    event.setEnd(endEventDateTime);
like image 149
user1825866 Avatar answered Nov 02 '22 20:11

user1825866