【发布时间】:2019-06-24 11:38:01
【问题描述】:
我试图解析一些包含日期条目作为纪元数值的 json 文档,使用后:
Document.parse(((JSONObject) o).toJSONString()) 日期格式自动设置为 ie。 Wed Nov 08 05:10:20 CET 2017
我需要不同的格式(也需要毫秒)
所以问题是,如何解析 ie。 "$date" -> "1510114220518" 到一些带有 Bson Document.parse 方法的自定义格式化字符串?
JSONArray arrJson = getJsonData(sFilePath);
for (Object o : arrJson) {
Document doc = Document.parse(((JSONObject) o).toJSONString());
编辑: 只是一个更新:问题出在 Document.parse 方法中,因为我不知道如何告诉 .parse 函数在解析 json 文档时使用自定义日期格式。我总是得到某种默认日期格式。 如何向 .parse 方法发送一些参数以返回包含所有数据和自定义日期格式的文档? 喜欢:
Format dateFormat = 'yyyy-MM-dd@HH:mm:ss.SSSZ'
Document doc = Document.parse(((JSONObject) o).toJSONString(dateFormat ));```
Im always getting ```Wed Nov 08 05:10:20 CET 2017``` format, **I need a Document with all data and all dates to be in yyyy-MM-dd@HH:mm:ss.SSSZ format** ?
This is sample of json document:
[
{ "_id" : { "user_id" : { "$oid" : "51b76b7459a273c8a6000ed8" }, "updated_at" : { "$date" : 1510114220518 } }, "count" : 153 },
{ "_id" : { "user_id" : { "$oid" : "51b76b7459a273c8a6000ed8" }, "updated_at" : { "$date" : 1511405948977 } }, "count" : 3 },
{ "_id" : { "user_id" : { "$oid" : "51b76b7459a273c8a6000ed8" }, "updated_at" : { "$date" : 1511405948991 } }, "count" : 153 }
and when I do the Document.parse()...date is in some default format like ie. Wed Nov 08 05:10:20 CET 2017
【问题讨论】:
-
您可能正在解析为
Date并请求具有特定格式的Date。这是不可能的。参见例如display Java.util.Date in a specific format。此外,Date类设计不佳且早已过时。您希望Instant来自 java.time, the modern Java date and time API,。 -
你确定返回的对象是
String吗?询问是因为Wed Nov 08 05:10:20 CET 2017看起来很像Date对象,即其toString方法的返回值。