Spring Mongodb Timestamp Timezone Misleading

11,001

Solution 1

MongoDB stores times in UTC by default, and will convert any local time representations into this form, see the documentation. You will have to compute the original local time in your application logic.

Solution 2

I am also using spring data with spring boot. Even if Mongo is storing my current timezone (CEST) in UTC, when I retrieve it with queries from a MongoRepository, it does the conversion automatically and retrieves the desired data.

Share:
11,001
fatiherdem
Author by

fatiherdem

Spring Framework Lover and Hater

Updated on June 27, 2022

Comments

  • fatiherdem
    fatiherdem almost 2 years

    I am using Spring Data MongoDB. When I save some records MongoDb doesn't save correctly my timestamp.

    Here is my timestamp field in Spring.

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private Date timestamp = new Date();
    

    My MongoDB record.

    {
    "_id": ObjectId("5697a672ce2a8e5347d86afd"),
    "batteryLevel": 100,
    "beaconClass": 3,
    "beaconId": "dsadsa",
    "timestamp": ISODate("2016-01-14T13:45:22.702Z")
    }
    

    When I log to console my timezone and date I see it is correct.

    Eastern European Time
    Asia/Istanbul
    Thu Jan 14 15:45:22 EET 2016
    

    How can I correct time MongoDB timestamp?

  • user666
    user666 over 5 years
    How can we tell mongo that we are sending the date in UTC so that it wont convert it again to UTC as it is already in UTC...I am using spring MongoRepository JPA