MongoDb BSON stores date in UTC

If I try to put a date field in a document (BSON) and write it to Mongo, BSON writes it to UTC. For example, date

DateTime dateTime = new DateTime("2015-07-01");
Document doc = new Document("date", dateTime.toDate());

will be saved as

"date" : ISODate("2015-06-30T18:30:00Z")

in mongo. And, if I extract it using the same Java driver, I get it as

Wed Jul 01 00:00:00 IST 2015

Great. Isn't that a solution? I mean, why can't I keep my date the way I want it? What if I need to request a database from another time zone? Am I getting different results? The date field is an important part of Mongo with a rich set of operators wrapped around it. However, why does Mongo not provide such flexibility? Thanks

+4
source share
4 answers

My problem is solved by setting my time zone as UTC from the code itself.

    DateTimeZone zone = DateTimeZone.UTC;
    DateTimeZone.setDefault(zone);
0

, . , , UTC. , mongo , .

, UTC, :

-Duser.timezone="UTC"
+5

DateTime ( org.joda.time java.time) , MongoDB, , . (. BSON)

UTC - - .

LocalDateTime. DateTimeZone, .

0

. UTC MongoDB.
, , :

new Date(fetchedDate);

.

0
source

All Articles