title: MongoDB Query by Date in Java
body: <p>I`m trying to query mongodb for documents where "date" is betwen two dates.
  Sample data is:</p>
  
  <pre><code>{
          "_id" : ObjectId("4fad0af6709fbc1d481c3e05"),
          "ID" : NumberLong("200930746205085696"),
          "text" : "Forgot my charger...:(",
          "date" : ISODate("2012-06-14T10:49:57Z"),
          "sentiment" : "NEG"
  }
  </code></pre>
  
  <p>My Java code is:</p>
  
  <pre><code>DBCursor cursor = null;
  DB db = connect();
  
      Date startDate = new Date(System.currentTimeMillis() - (long)(numOfTimePeriods+1)*time);
      Date endDate = new Date(System.currentTimeMillis() - (long)numOfTimePeriods*time);
      DBObject query = new BasicDBObject();
      query.put("date", new BasicDBObject("$gt", startDate).append("$lte", endDate));
  
      cursor = db.getCollection("status").find(query);
  </code></pre>
  
  <p>but the cursor object has no results.</p>
  
  <p>Query object looks like this:</p>
  
  <pre><code>{ "date" : { "$gt" : { "$date" : "2012-05-15T00:16:15.184Z"} , "$lte" : { "$date" : "2012-06-14T10:16:15.184Z"}}}
  </code></pre>
  
  <p>I suspect the problem is the date representation in DB.
  Any suggestions on this?</p>