gpt4 book ai didi

java - 将从 Google Tasks API 返回的 Android 中的 RFC 3339 日期时间对象转换为字符串

转载 作者:太空狗 更新时间:2023-10-29 14:26:43 25 4
gpt4 key购买 nike

我正在使用 Google Tasks API 返回值,例如任务标题、任务备注和任务截止日期。

当我尝试将截止日期 ( stored as RFC 3339) 输出为字符串时,我得到了 java.lang.IllegalArgumentException

我正在尝试使用 SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") 进行此转换。

我的猜测是 SimpleDateFormat 不是可行的方法,但我尝试过的所有其他转换都失败了。我对使用 Joda ISODateFormat 持开放态度,但即使那样也行不通。我已经包含了相关的代码 fragment 。

我的任务(调用项)类定义是:

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;
}

}

为了看到这个错误,我演示了 doInBackground 方法,我尝试输出返回日期:

@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();
}
}

编辑:

根据要求,这里是堆栈跟踪:

[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)]

这是任务资源:

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

最佳答案

Google Java 客户端库辅助函数似乎是 import com.google.api.client.util.DateTime;

已经返回一个文本字符串,不需要做转换。这出乎我的意料!

关于java - 将从 Google Tasks API 返回的 Android 中的 RFC 3339 日期时间对象转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11739592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com