[英]Parsing json date to specific date format
我試圖解析一些包含日期條目的json文檔作為epoch數值,使用后: Document.parse(((JSONObject) o).toJSONString())
日期格式自動設置為ie。 Wed Nov 08 05:10:20 CET 2017
我需要不同的格式(也需要一毫秒)
所以問題是,如何解析即。 "$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方法發送一些參數來返回包含所有數據的文檔和CUSTOM DATE FORMAT? smtng喜歡:
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
據我所知,你可以使用Java的SimpleDateFormat類。 例如:
long time = 1510114220518;
SimpleDateFormat simpleFormat = new SimpleDateFormat ("yyyy.MM.dd 'at' hh:mm:ss");
Date date = new Date(time);
System.out.println(simpleFormat.format(date));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.