获取 jsonobject 数据上的第一个值日期,格式为 datetime yyyy-MM-dd'T'HH:mm:ss。党卫军



当我想在JSONObject上获得日期类型的数据时,我遇到了一些问题

我有这样的日期:

    {
"error": false,
"alerts": {
    "code": "200",
    "message": "retrieve success"
},
"data": [{
    "incident__task_i_d": "8d37a3f1-d257-4833-9701-4eab0e48076f",
    "address": "Pulo Jahe Motor",
    "task_status": "Completed",
    "incident_i_d": "83eeed0f-4ea4-416b-8721-637e40ac9f72",
    "vehicle_i_d": "36420155-6214-4745-bbf3-7dae7d50ce9c",
    "agent_i_d": "1c3436fd-9338-4910-9686-8c1cd0a33433",
    "dispatched_time": "2016-08-12T08:29:47.89",
    "origin_latitude": "-6.8979664",
    "origin_longitude": "107.6135752",
    "destination_latitude": "-6.198816382772501",
    "destination_longitude": "106.921986633551",
    "created_dt": "2016-08-12T08:29:47.89",
    "departed_time": "2016-08-12T10:13:20.947",
    "arrival_time": "2016-08-12T10:11:14.93",
    "kick_off_time": "2016-08-12T10:13:54.54",
    "finished_time": "2016-08-12T10:14:30.893",
    "incident_reported_dt": "2016-08-11T08:21:34.23"
}]
    }

我有解析日期,但不能正常工作。这是我的代码:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS");
JsonElement dispatched_time = ((JsonObject)jaray.get(i)).get("dispatched_time");
modelRekap.setDispatched_time(sdf.parse(String.valueOf(dispatched_time.toString())));

和我有一个错误,像这样:

java.text.ParseException: Unparseable date: ""2016-08-12T08:29:47.89"" (at offset 0)
有人能帮我吗?谢谢的

您面临的问题是基于'SimpleDateFormat'不知道在格式化时如何处理"T"。

所以你最好的选择(据我所知)是这样做:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SS");
JsonElement dispatched_time = ((JsonObject)jaray.get(i)).get("dispatched_time");
modelRekap.setDispatched_time(sdf.parse(dispatched_time.toString().replace("T", " ")));

相关内容

  • 没有找到相关文章

最新更新