mm martin mm martin - 1 year ago 103
Java Question

How to get the datetime from NumberLong

I received a JSON field from mondb:

"publishDate": NumberLong("1500948351000"),

I try to parse it as this in Java:

String docTime = new DateTime(jsonDoc.getLong(publishDate)).toString();

But receive an exception:

org.json.JSONException: JSONObject["publishDate"] is not a long.

How to retrieve the datetime from the NumberLong?

Tom Tom
Answer Source

'NumberLong("1500948351000")' is not a long.

String aa = jsonDoc.getString(publishDate) 

is probably the String with value NumberLong("1500948351000"). And then you can use

String longString = aa.subString(aa.indexOf("\"") + 1, aa.lastIndexOf("\""));
long pbDate = Long.parseLong(longString);

to obtain the long value.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download