0

Im trying to parse some json document which contains date entry as epoch numeric value, after using: Document.parse(((JSONObject) o).toJSONString()) date format is automatically set to ie. Wed Nov 08 05:10:20 CET 2017 I need different format (need a milliseconds also)

So question is, how to parse ie. "$date" -> "1510114220518" to some custom formatted string with a Bson Document.parse method ?

JSONArray arrJson = getJsonData(sFilePath);
            for (Object o : arrJson) { 
                Document doc = Document.parse(((JSONObject) o).toJSONString());

Edit: just an update: problem is in Document.parse method because I dont know how to tell .parse function to use custom date format when parsing json document. I always getting some kind of default date format. How to send to .parse method some argument to return me document with all data AND CUSTOM DATE FORMAT ? smtng like :

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
smandic
  • 63
  • 1
  • 1
  • 4
  • You are probably parsing into a `Date` and asking for a `Date` with a specific format. That is not possible. See for example [display Java.util.Date in a specific format](https://stackoverflow.com/questions/6262310/display-java-util-date-in-a-specific-format). Also the `Date` class is poorly designed and long outdated. You want `Instant` from [java.time, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) instead. – Ole V.V. Jun 24 '19 at 11:45
  • Possible duplicate of [converting long string to date](https://stackoverflow.com/questions/11753341/converting-long-string-to-date). I recommend [the answer using Java 8](https://stackoverflow.com/a/39164330/5772882). – Ole V.V. Jun 24 '19 at 11:49
  • Are you sure the returned object is a `String`? Asking because `Wed Nov 08 05:10:20 CET 2017` looks very much like a `Date` object, that is, the return value from its `toString` method. – Ole V.V. Jun 24 '19 at 19:47

1 Answers1

0

As far as I understand your question, you could just use Java's SimpleDateFormat class. For example:

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));
Javan
  • 189
  • 1
  • 7
  • 2
    Thanks for wanting to contribute. Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Jun 24 '19 at 11:45
  • @OleV.V. Thank you for the hint! I wasn't aware its outdated, since there are still a lot of guides pointing to this class. Great to learn something new! – Javan Jun 24 '19 at 12:10
  • True, unfortunately very many of the pages showing how to use these old classes are still around. – Ole V.V. Jun 24 '19 at 12:12
  • Tnx guys for response, but Im afraid I didnt explain you situation very well, I know how to manipulate with date formats when Im using date variable directly, but in this case I dont have a option because I cannot influence to bson Document.parse() ..it just parses my json objects and returns me a string with date format which is not acceptable to me. Please, see post update. – smandic Jun 24 '19 at 12:45