How to save LocalDate to MongoDB without time (why is mongo saving date with time even if i save just the date)?
Asked Answered
S

1

6

Im trying to save List< LocalDate > in mongo (just the date without time in yyyy-MM-dd format), After my API completes the calculation of datesList , im trying to save those dates in MongoDB. Upon saving ,i see that the dates are being saved in yyyy-MM-dd hh:mm:ss format as shown below.

enter image description here

Can someone let me know why is this happening? Is there a way to just save the LocalDate in yyyy-MM-dd format? Thank you so much.

Im using SpringJPA Mongo with springboot and Java.

Skerrick answered 5/2, 2020 at 23:35 Comment(0)
T
9

This is happening because MongoDB stores data in BSON format (see the BSON spec).

There is no datatype in BSON for Date, there is only Timestamp and UTC Datetime both of which are stored as a 64-bit integer. UTC datetime is the number of milliseconds since epoch, leading to the time portion being all zeroes when it is rendered.

If you want to store just the date, you'll need to use a string type.

If the issue is how the data is displayed, you'll just need a different function to convert the timestamp returned from MongoDB to the display format you want to use.

Tophole answered 6/2, 2020 at 0:28 Comment(1)
thanks for your answer. Ill try to convert it to string and save itSkerrick

© 2022 - 2024 — McMap. All rights reserved.