0

POJO:

public class POJO {

    private String IdpUploadId;
    private String type;
    private String userId;
    private String email;
    private String cnsTenantId;
    private Date createdOn;
    private String createdBy;
    private Date updatedOn;
    private String updateBy;

}

Here the Date is coming from the library java.util.date. When I insert the above pojo class into mongodb, the date fields are stored properly as "2022-06-13T14:20:51.511+00:00". However when I retrieve it, the date is being returned in the integer format like below. Check createdOn and updateBy fields.

{
        "type": "custom",
        "userId": "XX",
        "email": null,
        "cnsTenantId": "XX",
        "createdOn": 1655130051511,
        "createdBy": "USERANA002",
        "updatedOn": 1655130051511,
        "updateBy": "XX",
        "idpUploadId": "4961c827-0626-465e-9ec6-fd392c69e08b"
 }

The above data is retrieved in this way: MongoCollection collection = database.getCollection("collectionName", POJO.class);

How to retrieve the date in the proper date format ?

  • Please check here, you may find inspiration: https://stackoverflow.com/questions/49011711/java-get-returning-date-as-a-13-digit-number – Mihael Mihov Aug 23 '22 at 12:06
  • I strongly recommend you don’t use `Date`. That class is poorly designed and long outdated. For a point in time use `Instant` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 23 '22 at 16:28
  • 1
    @OleV.V. I have added the required jackson jsr dependency & registered the Object Mapper with the time module. The result of the JSON when retrieving Instant date value is still in the Integer form but the result for a LocalDateTime data type is like this createdOn": [ 2022, 8, 26, 5, 8, 4, 196000000 ]. I still didn't get in the Time stamp which I need. – Kotagiri Sathwik Aug 26 '22 at 06:44

0 Answers0