Google Calendar API (Java) - Invalid time format w

2019-08-07 14:46发布

I'm using Java Google API client and I'm trying to create a Google Calendar Event.My authentication and calendar service initialization works (I'm able to log in and fetch my calendars events). The problem occurs when creating the event. Here is my code:

    Event event = new Event();

    event.setSummary( title );

    event.setStart( new EventDateTime().setDate( new DateTime( new Date() ) ) );
    event.setEnd( new EventDateTime().setDate( new DateTime( new Date() ) ) );

    return _calendarService.events().insert( GOOGLE_CALENDAR_ID, event ).execute();

Here is the error I get:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request { "code" : 400, "errors" : [ { "domain" : "global", "message" : "Invalid value for: \"T\" found, can only parse bare date string: 2014-10-10T15:58:06.165+03:00", "reason" : "invalid" } ], "message" : "Invalid value for: \"T\" found, can only parse bare date string: 2014-10-10T15:58:06.165+03:00" }

Any ideas what I'm doing wrong here?

2条回答
We Are One
2楼-- · 2019-08-07 15:27

The problem is that you are doing setDate() and passing in a dateTime (which then results in the "T15:58:06.165" appended to your date). You can either do EventDateTime().setDateTime() if you want a timed event or if not, you can do something like:

new EventDateTime().setDate(new DateTime(true, new Date(), 0));

查看更多
贼婆χ
3楼-- · 2019-08-07 15:33

As documentation says, you need to pass timezone toDateTime object if it is not set on EventDateTime object(and in your case it is not set) using setTimeZone method.

So either create new DateTime object including timezone, or create new EventDateTime object and set its timezone before sending request to server.

查看更多
登录 后发表回答