2013-05-04 8 views
5

È possibile creare un evento a tempo utilizzando l'API di Google Calendar v3 di Java (come da codice di esempio sul sito Web di Google), ma è necessario creare un evento per tutto il giorno.Cercare di creare un evento per tutto il giorno utilizzando l'API di Google Calendar v3 per Java causa un errore

io chiamo setStart dell'evento() e setEnd(), vale a dire

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

Questi metodi richiedono e EventDateTime, cioè

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

uso il SetDateTime() metodi come setDate() cause un errore 404.

SetDateTime() richiede un oggetto com.google.api.client.util.DateTime, facendo

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

Passando nel fuso orario dà un elemento di tempo quindi non è un evento tutto il giorno.

Ho provato a installare dateOnly al vero, ma questo dà un errore:

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

non posso ottenere gli altri modi di creare DateTime al lavoro: data Data, zona TimeZone lungo valore Data valore valore a lungo, Integer tzShift valore String

Da che parte si crea DateTime e posso usare setDate(), vale a dire nuovi EventDateTime(). setDate (...)?

Qualcuno ha uno snippet di codice testato? Perché questo non è documentato da Google?

ps È interessante notare che, durante la lettura di eventi da Google, l'uso di getDate() provoca un'eccezione con eventi temporizzati e getDateTime() un'eccezione con eventi giornalieri. È necessario utilizzare getDate() per eventi giornalieri e getDateTime() per eventi temporizzati.

risposta

8

Risolto.

Per creare un evento per tutto il giorno, è necessario utilizzare setDate() che ha creato oggetti DateTime utilizzando una stringa (che ho creato formattando i miei oggetti Date). Il codice:

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); 
Problemi correlati