-1

I use java.util.Date() to get timestamps for insert to database. When I view the result, the timestamps in the database is differente from the system machine time.

  • java.util.Date() time: 22/10/2012 10:00:07 AM
  • System machine time: 22/10/2012 5:50:34 PM

I use MongoDB, here my code

Mongo mongo = new Mongo(DBConstants.DATABASE_HOST,  Integer.parseInt(DBConstants.DATABASE_PORT));
DB db = mongo.getDB("library");
DBCollection books = db.getCollection("books");
BasicDBObject book = new BasicDBObject();
book.put("name", "Understanding JAVA");
book.put("pages", 100);
book.put("amount",10);
book.put("timestamp", new java.util.Date());
books.insert(book);

Who can explain this to me?

Sonrobby
  • 3,112
  • 8
  • 30
  • 38
  • 1
    You are not putting the date into the database correctly or you are not displaying to date from the database correctly. – Peter Lawrey Oct 22 '12 at 10:09
  • 3
    Is the difference exactly 7 hours, 50 minutes and 27 seconds? Than it is unlikely to be a time zone problem... – Matthias Oct 22 '12 at 10:10
  • Are you executing `new Date()` in the same machine you're inserting into DB, or are you executing in a client and inserting in a server? We need more details to be able to answer you. – m0skit0 Oct 22 '12 at 10:10
  • are you looking for java.util.Date() time directly in database or you are printing to console?Also, make sure no default value is set for that column in the database(I would prefer null). – Sumit Desai Oct 22 '12 at 10:17
  • MongoDB and java program in the same machine! – Sonrobby Oct 22 '12 at 10:35

2 Answers2

0

It maybe happening because java.util.Date is timezone-independent.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
0

Please ensure you system machine and database are using the same timezone.

Drogba
  • 4,186
  • 4
  • 23
  • 31