I am somewhat new to Java and am trying to write a query which will return the documents stored in my mongodb between two dates. I think I am close to getting it correct but am having a hard time getting there. I am receiving the date ranges in the following format:
- [2017-06-29, 2017-07-05]
The dates that i am comparing are stored in the database as follows:
- yyyy-MM-dd'T'HH:mm:ss-SSSS
What i have so far..
public List<VehicleStatus> getReportingDateRange(List<String> dates) {
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss-SSSS");
Date startDate = outputFormat.parse(dates.get(0));
Date endDate = outputFormat.parse(dates.get(1));
Query query = new Query();
Criteria c = new Criteria().andOperator(Criteria.where("updateTime").gte(outputFormat.format(startDate)),
Criteria.where("updateTime").lte(outputFormat.format(endDate)));
query.addCriteria(c);
return this.mongoOperations.find(query, VehicleStatus.class);
}
I am receiving a parse Exception. I am really not sure where to go from here, any help is greatly appreciated. If you need any additional information please let me know.
Thank you!
Date startDate = inputFormat.parse(dates.get(0)); Date endDate = inputFormat .parse(dates.get(1));and your dates should be stored as date type. So no parse to output format is required andCriteria c = new Criteria().andOperator(Criteria.where("updateTime").gte(startDate), Criteria.where("updateTime").lte(endDate));String startDate = outputFormat.format(inputFormat.parse(dates[0])); String endDate = outputFormat.format(inputFormat.parse(dates[0]));and pass this to criteria