I need to create some events and upload them to a google calendar. Now I have hit an unforeseen bump, as the times and timezone offset gets changed automatically, once uploaded. I am working with a list of meetings, whose times should not be altered, indepedently of time zone or DST.
Since this topic has been touched upon here in stack overflow, I tried some of the solutions but they just created more confusing results.
Here are four events I created with different approaches:
- Creating an event with time zone and offset
- Creating an event with time zone but without offset
- Creating an event without time zone, but with offset
- Creating an event by uploading a CSV through the web interface
Creating an event with time zone and offset:
static void dstUploadProbe(){
//Initiate helper class to create OAuth2 token and get Calendar service
CalendarHelper calendarHelper = new CalendarHelper();
calendarHelper.initService();
Event event = new Event();
//event start time with time zone and offset
DateTime startDateTime = new DateTime("2022-10-30T18:00:00+02:00");
EventDateTime startEventDateTime = new EventDateTime();
startEventDateTime.setDateTime(startDateTime);
startEventDateTime.setTimeZone("Europe/Berlin");
//end time
DateTime endDateTime = new DateTime("2022-10-30T20:30:00+02:00");
EventDateTime endEventDateTime = new EventDateTime();
endEventDateTime.setDateTime(endDateTime);
endEventDateTime.setTimeZone("Europe/Berlin");
//create event
event.setSummary("TestMeeting 1");
event.setStart(startEventDateTime);
event.setEnd(endEventDateTime);
//upload event
try {
CalendarHelper.addEvent(calendarHelper.service,calendarHelper.currentCalendarId,event);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
Creating an event with time zone WITHOUT offset
static void dstUploadProbe(){
//Initiate helper class to create OAuth2 token and get Calendar service
CalendarHelper calendarHelper = new CalendarHelper();
calendarHelper.initService();
Event event = new Event();
//event start time with time zone WITHOUT offset
DateTime startDateTime = new DateTime("2022-10-30T18:00:00");
EventDateTime startEventDateTime = new EventDateTime();
startEventDateTime.setDateTime(startDateTime);
startEventDateTime.setTimeZone("Europe/Berlin");
//end time
DateTime endDateTime = new DateTime("2022-10-30T20:30:00");
EventDateTime endEventDateTime = new EventDateTime();
endEventDateTime.setDateTime(endDateTime);
endEventDateTime.setTimeZone("Europe/Berlin");
//create event
event.setSummary("TestMeeting with tz but no offset");
event.setStart(startEventDateTime);
event.setEnd(endEventDateTime);
//upload event
try {
CalendarHelper.addEvent(calendarHelper.service,calendarHelper.currentCalendarId,event);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
Creating an event without time zone, but with offset
static void dstUploadProbe(){
//Initiate helper class to create OAuth2 token and get Calendar service
CalendarHelper calendarHelper = new CalendarHelper();
calendarHelper.initService();
Event event = new Event();
//event start time with time zone WITHOUT offset
DateTime startDateTime = new DateTime("2022-10-30T18:00:00");
EventDateTime startEventDateTime = new EventDateTime();
startEventDateTime.setDateTime(startDateTime);
//end time
DateTime endDateTime = new DateTime("2022-10-30T20:30:00");
EventDateTime endEventDateTime = new EventDateTime();
endEventDateTime.setDateTime(endDateTime);
//create event
event.setSummary("TestMeeting with offset WITHOUT tz");
event.setStart(startEventDateTime);
event.setEnd(endEventDateTime);
//upload event
try {
CalendarHelper.addEvent(calendarHelper.service,calendarHelper.currentCalendarId,event);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
The uploaded CSV file contents:
Subject, Start Date, Start Time, End Time
Meeting uploaded in CSV,30/10/2022,06:00 PM, 08:30 PM
Here are the results as shown in the Calendar web view:
... and the resulting events, listed programatically.
Upcoming events
TestMeeting 1 (2022-10-30T17:00:00.000+01:00)
Meeting uploaded in CSV (2022-10-30T18:00:00.000+01:00)
TestMeeting with offset WITHOUT tz (2022-10-30T19:00:00.000+01:00)
TestMeeting with tz but no offset (2022-10-30T19:00:00.000+01:00)
Note how the time offsets have all been changed to +01:00 to compensate a change of DST. The only result that is as intended is the last one (CSV upload), but alas, that has not been reached using google api.
How can I navigate these waters without having to reinvent time?
