[JAVA-3336] Date is not fetched as it is stored - possible Reader problem? Created: 26/Jun/19  Updated: 27/Oct/23  Resolved: 26/Jun/19

Status: Closed
Project: Java Driver
Component/s: BSON
Affects Version/s: 3.10.2
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Dario Nuevo Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MongoDB v3.6.5

openjdk version "1.8.0_212"
OpenJDK Runtime Environment (build 1.8.0_212-8u212-b03-0ubuntu1.19.04.2-b03)
OpenJDK 64-Bit Server VM (build 25.212-b03, mixed mode)

Linux 5.0.0-17-generic #18-Ubuntu SMP Tue Jun 4 15:34:08 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux


Attachments: PNG File Screenshot from 2019-06-26 14-34-29.png    

 Description   

Hi guys..

We are using the driver in an ETL project that converts dates. I'm a bit puzzled with this behavior.

We inserted this record:

{
	"_id" : "ZRH",
	"id" : "ZRH",
	"name" : "Zürich",
	"population" : 300000,
	"factor" : 0.81191,
	"createDate" : ISODate("1245-04-28T13:27:32.000Z"),
	"createDateString" : ISODate("1984-05-02T00:00:00.000Z"),
	"isGood" : false,
	"countryExtRef" : {
		"ref" : DBRef("Country", "CHE")
	},
	"_loadModifiedData !Screenshot from 2019-06-26 14-34-29.png|thumbnail! " : false,
	"_loadCreatedAt" : ISODate("2019-06-26T12:16:48.524Z"),
	"recordOrigin" : "faker"
},

The problem is with the "createDate" field - as you see it's 1245-04-28T13:27:32.000Z

Later in the application (well, in our integration tests), i fetch the entity back:

Document singleRecord = city.find(eq("_id", "ZRH")).first();

I get this:

So what I see saved in MongoDB is ISODate("1245-04-28T13:27:32.000Z") - I get back "Fri Apr 21 13:27:32 UTC 1245" - somehow 7 days were lost?

Sadly I lack the ability to really diagnostic this problem - I see that the BSON reader gets the value "-23973085975000" in readInt64() for the specific datetime. This indeed is the rendered Date (April 21).. Which is strange as mongo shell shows me April 28 (which is what I inserted)..

Any ideas what could be wrong? Thank you so much!



 Comments   
Comment by Dario Nuevo [ 26/Jun/19 ]

@ross.lawley OK; appreciate your input! thanks!

Comment by Ross Lawley [ 26/Jun/19 ]

Hi dario.nuevo,

Thanks for the ticket. Unfortunately, there are many issues with Java Date class and this shows an issue crossing calendars. Prior to 1582 Date will use the Julian calendar and after it will use the Gregorian - What you are seeing is differences in date representations between these calendars.

Happily, the underlying data is stored in MongoDB as ms since epoch and when using Java Instant classes (which always use the Gregorian calendar) it will correct itself:

System.out.println(collection.find().first().getDate("createDate").toString());
// Outputs: Fri Apr 21 13:27:32 GMT 1245  - uses the julian calendar as its pre 1582
 
System.out.println(collection.find().first().getDate("createDate").toInstant().toString());
// 1245-04-28T13:27:32Z  - uses the gregorian calendar

Just to let you know for future reference the best place for questions regarding MongoDB usage or the Java driver specifics is the mongodb-user mailinglist or stackoverflow as you will reach a broader audience there. If your business requires an answer from MongoDB within a time frame then we do offer production support.

I hope that helps,

Ross

Generated at Thu Feb 08 08:59:21 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.