Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

397
Views
Spring Data MongoDB - when saving LocaDate/LocalDateTime the value is being set to yesterday

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

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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 
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!