I am trying to change the output date format for my google calendar code, but it cannot be used in SimpleDateFormater because it is a google api date, not the java version.
I did get it to work but eventually removed that code because it always returned null.
my app activity would display it as "(my event) on Null", where my event would update but the date always returned null.
Im not great at working with googles API and am newish to java, so any help will be appreciated!
Here is my code:
import android.os.AsyncTask;
import com.google.api.client.googleapis.extensions.android.gms.auth.GooglePlayServicesAvailabilityIOException;
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;
import com.google.api.client.util.DateTime;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.Events;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* An asynchronous task that handles the Google Calendar API call.
* Placing the API calls in their own task ensures the UI stays responsive.
*/
/**
* Created by miguel on 5/29/15.
*/
public class ApiAsyncTask3 extends AsyncTask<Void, Void, Void> {
private MainActivity mActivity3;
/**
* Constructor.
* @param activity MainActivity that spawned this task.
*/
ApiAsyncTask3(MainActivity activity) {
this.mActivity3 = activity;
}
/**
* Background task to call Google Calendar API.
* @param params no parameters needed for this task.
*/
@Override
protected Void doInBackground(Void... params) {
try {
mActivity3.clearResultsText();
mActivity3.updateResultsText(getDataFromApi());
} catch (final GooglePlayServicesAvailabilityIOException availabilityException) {
mActivity3.showGooglePlayServicesAvailabilityErrorDialog(
availabilityException.getConnectionStatusCode());
} catch (UserRecoverableAuthIOException userRecoverableException) {
mActivity3.startActivityForResult(
userRecoverableException.getIntent(),
BlockOrderGCalDaily.REQUEST_AUTHORIZATION);
} catch (IOException e) {
mActivity3.updateStatus("The following error occurred: " +
e.getMessage());
}
return null;
}
/**
* Fetch a list of the next 10 events from the primary calendar.
* @return List of Strings describing returned events.
* @throws IOException
*/
private List<String> getDataFromApi() throws IOException {
// List the next 10 events from the primary calendar.
DateTime now = new DateTime(System.currentTimeMillis());
List<String> eventStrings = new ArrayList<String>();
Events events = mActivity3.mService.events().list("9sfoekroead5j1sb8aduqgvog4@group.calendar.google.com")
.setMaxResults(1)
.setTimeMin(now)
.setOrderBy("startTime")
.setSingleEvents(true)
.execute();
List<Event> items = events.getItems();
for (Event event : items) {
DateTime start = event.getStart().getDateTime();
if (start == null) {
// All-day events don't have start times, so just use
// the start date.
start = event.getStart().getDate();
}
eventStrings.add(
String.format("%s on %s", event.getSummary(), start));
}
return eventStrings;
}
}