[英]Google Calendar API (Java) - Invalid time format when creating Google Calendar Event
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). 我正在使用Java Google API客户端,并尝试创建Google Calendar Event。我的身份验证和日历服务初始化有效(我可以登录并获取我的日历事件)。 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" }
com.google.api.client.googleapis.json.GoogleJsonResponseException:400错误的请求{“代码”:400,“错误”:[{“域”:“全局”,“消息”:“的无效值:\\” T \\“找到,只能解析裸日期字符串:2014-10-10T15:58:06.165 + 03:00”,“ reason”:“ invalid”}],“ message”:“无效值:\\” T \\“找到,只能解析裸日期字符串:2014-10-10T15:58:06.165 + 03:00“}
Any ideas what I'm doing wrong here? 有任何想法我在这里做错了吗?
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). 问题是您正在执行
setDate()
并传入dateTime(然后将“ T15:58:06.165”添加到您的日期)。 You can either do EventDateTime().setDateTime()
if you want a timed event or if not, you can do something like: 如果需要定时事件,可以执行
EventDateTime().setDateTime()
,如果不需要,可以执行以下操作:
new EventDateTime().setDate(new DateTime(true, new Date(), 0));
As documentation says, you need to pass timezone to DateTime
object if it is not set on EventDateTime
object(and in your case it is not set) using setTimeZone
method. 如文档所述 ,如果未使用
setTimeZone
方法在EventDateTime
对象上设置时区(并且在您的情况下未设置),则需要将时区传递给DateTime
对象。
So either create new DateTime
object including timezone, or create new EventDateTime
object and set its timezone before sending request to server. 因此,要么创建新的
DateTime
对象(包括时区),要么创建新的EventDateTime
对象并设置其时区,然后EventDateTime
服务器发送请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.