Jesus Zuñiga
Jesus Zuñiga

Reputation: 135

How can i parse BSON Timestamp in java

Hello guys i have this Bson Timestamp:

"ts" : { "$timestamp" : { "t" : 1562880169, "i" : 1 } }

How can i get the "t" and "i" values and parse them into variables in java.

I'm getting the Timestamp like this :

 BsonTimestamp timeStamp = (BsonTimestamp) currentDoc.get("ts");

Thank you very much.

Upvotes: 0

Views: 1367

Answers (2)

Arpan Kanthal
Arpan Kanthal

Reputation: 503

The BsonTimestamp object has getInc and getTime and getInc methods to extract those values. http://mongodb.github.io/mongo-java-driver/3.6/javadoc/org/bson/types/BSONTimestamp.html

To get the time, you would do a timeStamp.getTime() and for 'i' you would do a timeStamp.getInc()

Upvotes: 1

Amerousful
Amerousful

Reputation: 2555

JSONObject newObject = new JSONObject(bsonString);
System.out.println(newObject.get("t"))

Upvotes: 1

Related Questions