1

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!

4
  • Parse 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 and Criteria c = new Criteria().andOperator(Criteria.where("updateTime").gte(startDate), Criteria.where("updateTime").lte(endDate)); Commented Jul 10, 2017 at 14:56
  • @Veeram the dates are stored as a String in the DB Commented Jul 10, 2017 at 14:59
  • oh okay. try String startDate = outputFormat.format(inputFormat.parse(dates[0])); String endDate = outputFormat.format(inputFormat.parse(dates[0])); and pass this to criteria Commented Jul 10, 2017 at 15:07
  • @Veeram - that did the trick! Thanks so much for the help. Simple fix but something that i was clearly overlooking. Make an answer and i will make it as correct :) Commented Jul 10, 2017 at 15:18

1 Answer 1

4

You will need to parse the date into input format followed by formatting the date to output format.

Something like

String startDate = outputFormat.format(inputFormat.parse(dates[0])); 
String endDate = outputFormat.format(inputFormat.parse(dates[0]));

Criteria c = new Criteria().andOperator(Criteria.where("updateTime").gte(star‌​tDate), Criteria.where("updateTime").lte(endDate));

You should try to save the date as date type and use below version.

Date startDate = inputFormat.parse(dates.get(0)); 
Date endDate = inputFormat .parse(dates.get(1));

Criteria c = new Criteria().andOperator(Criteria.where("updateTime").gte(star‌​tDate), Criteria.where("updateTime").lte(endDate));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.