Search code examples
javaandroidjodatimedatetime-formatgoogle-tasks-api

Coverting RFC 3339 DateTime Objects in Android returned from Google Tasks API to String


I am using the Google Tasks API to return values, such as task title, task note, and task due date.

When I try to output the due date (stored as RFC 3339) as a string I get java.lang.IllegalArgumentException.

I am trying to use SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") to do this conversion.

My guess is that SimpleDateFormat is not the way to go but every other conversion I've tried has failed. I'm open to using Joda ISODateFormat but even that didn't work. I have included the relevant snippets of code.

My task (call item) class definition is:

import com.google.api.client.util.DateTime;

public class Item {

Long id;
String title;   
String description;
String extId1;
Integer status;
DateTime dateDue;
DateTime dateLastEdit;
DateTime dateCompleted;

Item parent;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescription() {
    return description;
}

public void setNotes(String description) {
    this.description = description;
}

public String getExtId1() {
    return extId1;
}

public void setExtId1(String extId1) {
    this.extId1 = extId1;
}

public Integer getStatus() {
    return status;
}

public void setStatus(Integer status) {
    this.status = status;
}

public DateTime getDateDue() {
    return dateDue;
}

public void setDateDue(DateTime dateDue) {
    this.dateDue = dateDue;
}

public DateTime getDateLastEdit() {
    return dateLastEdit;
}

public void setDateLastEdit(DateTime dateLastEdit) {
    this.dateLastEdit = dateLastEdit;
}

public DateTime getDateCompleted() {
    return dateCompleted;
}

public void setDateCompleted(DateTime dateCompleted) {
    this.dateCompleted = dateCompleted;
}

public Item getParent() {
    return parent;
}

public void setParent(Item parent) {
    this.parent = parent;
}

}

To see this go wrong I demonstrate the doInBackground method where I try to output a returned date:

@Override
protected List<Item> doInBackground(Void... arg0) {
  List<Item> itemList = new ArrayList<Item>();
  DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  Log.v(TAG, "Start doInBackground...");
  try {
      List<String> result = new ArrayList<String>();
      com.google.api.services.tasks.Tasks.TasksOperations.List listRequest = service.tasks().list("@default");
      listRequest.setFields("items/title,items/notes,items/due");
      List<Task> tasks = listRequest.execute().getItems();
      if (tasks != null) {            
          for (Task task : tasks) {
              result.add(task.getTitle());
              Item item = new Item();               
              item.setTitle(task.getTitle());
              item.setNotes(task.getNotes());
              DateTime due = task.getDue();
              item.setDateDue(due);
              Log.v(TAG, "Due date string: " + formatter.format(due));
              itemList.add(item);
          }
      } else {
          Log.v(TAG, "End doInBackground with no tasks");
          result.add("No tasks.");
      }       
      Log.v(TAG, "End doInBackground with tasks received");
      return itemList;           
  } catch (IOException e) {
      Log.v(TAG, "End doInBackground with IOException...");
      tasksSample.handleGoogleException(e);
      Item item = new Item();
      item.setTitle(e.getMessage());
      itemList.add(item);
      return itemList;
      //return Collections.singletonList(e.getMessage());
  } catch (Exception e2) {
      Log.v(TAG, "End doInBackground with generic exception...");
      e2.printStackTrace();
      Item item = new Item();
      item.setTitle(e2.getMessage());
      itemList.add(item);
      return itemList;
  } finally {
      tasksSample.onRequestCompleted();
  }
}

EDIT:

As requested, here is a stack trace:

[java.text.DateFormat.format(DateFormat.java:365), java.text.Format.format(Format.java:93), com.google.api.services.samples.tasks.android.AsyncLoadTasks.doInBackground(AsyncLoadTasks.java:78), com.google.api.services.samples.tasks.android.AsyncLoadTasks.doInBackground(AsyncLoadTasks.java:1), android.os.AsyncTask$2.call(AsyncTask.java:264), java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305), java.util.concurrent.FutureTask.run(FutureTask.java:137), android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208), java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076), java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569), java.lang.Thread.run(Thread.java:856)]

Here is the task resource:

{"due":"2012-07-04T00:00:00.000Z","notes":"note1","title":"test1"}


Solution

  • It seems that the Google Java client library helper function import com.google.api.client.util.DateTime;

    already returns a text string, no need to do a conversion. That was not was I was expecting!