Skip to content
Advertisement

How to read date (Timestamp) from MongoDB using Java

Am trying to read date field from MongoDB in below format

JavaScript

In the result 2017-01-23 22:16:07.000000, instead of 000 it should be the 812 (Original value: 2017-01-23-10.46.07.812000)

Note: Using MongoDB Java driver 3.4.

Thank you in advance!

Bharathi

Advertisement

Answer

You can use Java’s SimpleDateFormat to format the date accordingly. For example, assuming you inserted the date in MongoDB using the proper ISODate type:

JavaScript

This code prints the correct date:

JavaScript

Output is:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement