Spring-boot: mongoDB date comparison not working

旧城冷巷雨未停 提交于 2019-12-11 07:07:14

问题


I am trying to get appointments for a particular date.

So I am passing fromDate and toDate to find data in that date range.

Here is my code, in which query method is changing the actual date passing to it.

I have also pasted the query formed by this code in spring boot.

DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS\'Z\'");

fromDate = "2017-10-06T00:00:00.000Z";
toDate = "2017-10-07T23:00:00.000Z";        

Date startDate,endDate;

startDate = dateFormatter.parse(fromDate);
endDate = dateFormatter.parse(toDate);

System.out.println(startDate);
System.out.println(endDate);

Query q = new Query().addCriteria(new Criteria().orOperator(
          new Criteria().andOperator(Criteria.where("fromDate").gte(startDate),
          Criteria.where("fromDate").lte(endDate)),
          new Criteria().andOperator(Criteria.where("toDate").gte(startDate),
          Criteria.where("toDate").lte(endDate))
        ));

System.out.println(startDate);
System.out.println(endDate);

System.out.println(q); //here in query m getting different date

List<Appointment> result= mongoTemplate.find(q, Appointment.class);

System.out.println(result);

When I am trying to print query, it prints the following json which is wrong:

{
    "$or": [
        {
            "$and": [
                {
                    "fromDate": {
                        "$gte": {
                            "$date": "2017-10-05T18:30:00.000Z"  //expected date 2017-10-06
                        }
                    }
                },
                {
                    "fromDate": {
                        "$lte": {
                            "$date": "2017-10-07T17:30:00.000Z"
                        }
                    }
                }
            ]
        },
        {
            "$and": [
                {
                    "toDate": {
                        "$gte": {
                            "$date": "2017-10-05T18:30:00.000Z"
                        }
                    }
                },
                {
                    "toDate": {
                        "$lte": {
                            "$date": "2017-10-07T17:30:00.000Z"
                        }
                    }
                }
            ]
        }
    ]
}

My expected date were "2017-10-06T00:00:00.000Z" and "2017-10-07T23:00:00.000Z".


回答1:


You have to set the timezone to UTC when using DateFormat to parse string dates.

Alternatively you can use Instant in Java 8.

I have shown both examples.

endDate using dateFormatter with timezone set to UTC

startDate using Instant

Something like

DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS\'Z\'");
dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

Date startDate,endDate;

startDate =  Date.from(Instant.parse("2017-10-06T00:00:00.000Z"));
endDate = dateFormatter.parse("2017-10-07T23:00:00.000Z");


来源:https://stackoverflow.com/questions/46674501/spring-boot-mongodb-date-comparison-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!