date in MongoDB: when inserting Date objects into Mongo database, the date becomes 1 day earlier than itself
Asked Answered
P

3

9

My date string format is like this: Jan 2, 2012 After the Instant.parse() method, instant instance becomes the date of Jan 1, 2012, which is 1 day earlier, why? If the original date string is jan 1, 2012, the Instant will be the date of Dec 31, 2011.

String dateString="Jan 1, 2012";
Instant instant = Instant.parse(dateString, new DateTimeFormatterBuilder()
.appendMonthOfYearShortText()
.appendLiteral(" ")
.appendDayOfMonth(1)
.appendLiteral(", ")
.appendYear(4, 4)
.toFormatter());

DateTime dateTime = new DateTime(instant);
Date date = new Date(dateTime.getMillis());

document.append("time", new Date(dateTime.getMillis()));
tagsDbCollection.insert(document);

I'm using MongoDB to store these dates. I've tested and it shows when formatting date string->instant there's no mistake. But when I insert this Date type object into MongoDB, the date string in the MongoDB becomes 1 day earlier., why?

In MongoDB:

 /* 0 */
    {
      "_id" : ObjectId("50221a40da74d74053abb445"),
      "time" : ISODate("2011-12-31T14:00:00Z")
    }
Petrochemistry answered 5/8, 2012 at 8:55 Comment(1)
Have you tried adding a timezone component? It might be something with your local and the date getting offset by your timezone.Dogma
A
1
final String dateString = "Jan 2, 2012";
final DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendMonthOfYearShortText().appendLiteral(" ").appendDayOfMonth(1).appendLiteral(", ").appendYear(4, 4).toFormatter();
final DateTime jodaDate = dtf.parseDateTime(dateString);
System.out.println(jodaDate);
final Date javaDate = new Date(jodaDate.getMillis());
System.out.println(javaDate); 

Output is

2012-01-02T00:00:00.000+02:00
Mon Jan 02 00:00:00 EET 2012  

Next for:

final String dateString = "Jan 1, 2012";

output is:

2012-01-01T00:00:00.000+02:00
Sun Jan 01 00:00:00 EET 2012
Azazel answered 7/8, 2012 at 20:55 Comment(4)
Thanks for your reply. Yes, date string->Instant is no wrong. It is when I insert these dates into MongoDB, this problem comes out. Still don't know why...I've updated the question.Petrochemistry
May be you have TimeZone -10. So saved time si 14:00 previous day. When I working with MongoDB, I save date like long value in millisAzazel
Yes it is a time zone issue. I'm in Brisbane, Australia. It is UTC+10. This page says "MongoDB stores all DateTimes in UTC. Any local times you supply are converted to UTC when stored in the database". And I found after inserting when I query the dates from MongoDB via its Java API, the dates are converted to local time again. So it's OK now. Thank you.Petrochemistry
@liya if the format is "10-05-2013" then how the DateTimeFormatter looks like????Vespertine
H
1

Mongo stores its Dates in milliseconds since the Unix epoch.

See: http://www.mongodb.org/display/DOCS/Dates

So you dont have any time zone. But, if you use the console the .js parser is converting the UTC Dates into your current system time zone settings.

You can test that:

  • Create an Entity with some Date data.
  • then querying it via the console. (use String())
  • then exit the console and reconfigure the system time zone (debian/ubuntu: sudo dpkg-reconfigure tzdata )
  • then enter the console again and query your old data => you get the same UTC but different toString() outputs
Helmholtz answered 8/8, 2012 at 14:24 Comment(0)
P
0

You could check the UTC time zone, basically mongo server running depending on UTC time zone

SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); format.setTimeZone(TimeZone.getTimeZone("UTC"));

Phenology answered 30/6, 2016 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.