0

My requirement is to set BSONDateTime to specific data only. From java.util.Date i found exact data, but when trying to convert in BSONDateTime my output is wrong. Following is my code for convert:

val currentDate: LocalDate = LocalDate.now();
val milliSeconds: Long = currentDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
new BSONDateTime(milliSeconds);

Output:

Aspected: Fri Oct 16 2015 00:00:00

Actual: Fri Oct 15 2015 18:30:00

Update Question

My Basic requirement is that, i want set and compare my date in mongodb data using query. Like: my mongodb document have following structure

{
 "_id" : ObjectId("561e62892d0000e98ec782c3"),
 "addedOn" : ISODate("2015-10-14T14:11:21.361Z"),
 "expiredOn" : ISODate("2015-10-30T00:00:00.000Z")
}

My First Query requirement is set expire date to current date using below code:

 def currentDateOnly: BSONDateTime = {
  val currentDate: LocalDate = LocalDate.now();
  val milliSeconds: Long = currentDate.atStartOfDay().atZone(ZoneOffset.UTC).toInstant().toEpochMilli();

  logger.info("currentDateOnly Conversion: "+currentDate); 
  new BSONDateTime(milliSeconds);
}

From this code, i found current date with 12 am morning time. I am converting date to milliSeconds and create BSONDateTime object and set the value using reactive mongo query as below:

$set(propertyName -> CustomUtility.currentDateOnly) // this is just a set par of update statement.

My Second Query requirement is to match "expiredOn" date using following query:

$doc("expiredOn" $gte CustomUtility.currentDateOnly);

Now, when i set the date in database, my mongo db store invalid value of date, that i also mention above like:

Output:

Aspected: Fri Oct 16 2015 00:00:00

Actual: Fri Oct 15 2015 18:30:00

I need to store specific data and compare query with specific date.

Harmeet Singh Taara
  • 6,483
  • 20
  • 73
  • 126

0 Answers0