Reputation: 351
I've got a problem with saving Java8 Date API values to MongoDB database. Whenever a new LocalDate instance (LocalDate.now()) is being saved, as a result we are getting value with yesterdays date with time set to 23:00 PM. Example:
dt.getDate().toString()
is giving me "2017-03-17"
but when i look into the database i have a value like this:
"dt" : ISODate("2017-03-16T23:00:00.000Z")
My time zone is UTC+01:00
Upvotes: 7
Views: 9814
Reputation: 75964
MongoDB saves the date in UTC time.
LocalTime is your wall clock time.
When you pass it to MongoDb, spring will convert the LocalTime
to Instant
(UTC time) by using your System Zone.
Consider
LocalDateTime localDateTime = LocalDateTime.parse("2017-03-17T00:00:00")
Something like this happpens
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
This is where your local time is changed to UTC instant by applying offset (+01:00) from your local time.
Output(in UTC) : 2017-03-16T23:00:00.000Z
Upvotes: 10