title: JavaMail search by ReceivedDate, doesn't work down to the second?
body: <p>In IMAP, message numbering is based on when a message is placed into a folder (i.e. first message in the folder is 1, second message in is 2, etc.). However, this ordering has no regard for a message's received date. What I'm concerned about are messages older than message number 1 of the folder, i.e. a message that's been moved into a folder by a user manually (and not by when it was received into the system). </p>
  
  <p>I cannot just get every message's receivedDate, as this is horrendoulsy inefficent.</p>
  
  <p>I <em>thought</em> I could do a JavaMail search to get all messages with a received date <em>older</em> than the first message's received date, (and likewise then do a search to get all message with a recieved date <em>newer</em> than the first message's received date... which would be many, but while I need to process ALL of the older ones, I only need to process a few of the newer ones, (and by process, I mean download its headers).</p>
  
  <p>However, when I am using the search, it just doesn't seem to work right.</p>
  
  <pre><code>        Message[] messages = csqFolder.getMessages();           
  
          if (messages != null &amp;&amp; messages.length &gt; 0) {
  
              Date receivedDate = messages[0].getReceivedDate();
              log.trace("Message 1 receivedDate: &lt;" + receivedDate.toString() + "&gt;");
              SearchTerm msgsOlderThanOETFirst = 
                  new ReceivedDateTerm(DateTerm.LT, receivedDate);
  
              SearchTerm msgsNewerThanFirst = 
                  new ReceivedDateTerm(DateTerm.GT, receivedDate);
  
              Message[] oldMsgs = csqFolder.search(msgsOlderThanOETFirst, messages);
              log.trace("Size of oldMsgs: &lt;" + oldMsgs.length + "&gt;");
              Message[] newMsgs = csqFolder.search(msgsNewerThanFirst, messages);
              log.trace("Size of newMsgs: &lt;" + newMsgs.length + "&gt;");
  </code></pre>
  
  <p>However, when I run these searches, it does not seem to give the correct results.</p>
  
  <p>On a folder with 8 messages, 7 of which have a received date time of around 12:00 pm on Aug 5, but the first message in the folder which is at 4:00 pm on Aug 5, we will see the following:</p>
  
  <pre><code>Message 1 receivedDate: &lt;Fri Aug 05 16:46:57 CDT 2011&gt;
  Size of oldMsgs: &lt;0&gt;
  Size of newMsgs: &lt;7&gt;
  </code></pre>
  
  <p>However, ALL of the remaining seven messages are older than the first message... they should ALL be in oldMsgs.</p>
  
  <p>That said, IF I put a message from the previous day (Aug 4) in that folder, then the search will coreectly work... for that ONE message. It's like the search only works on a day-by-day basis, not down to the second...</p>
  
  <p>I should note that in the term msgsOlderThanOETFirst, I originally was using LE (hence the name), however, that would flip the above resutls -- ALL the messages would then be found in oldMsgs. </p>
  
  <p>Can anyone else confirm if this behavior is true, and if so, would it point to a bug in Java?</p>
  
  <p>I tried to look into the source code of search but I think I was getting a command line version of it and not the kind that the javamail package uses....</p>
  
  <p>I'm using MS Exchange 2007 with SP1, and JavaMail 1.4.3. </p>
  
  <p>Thanks for any suggestions.</p>
  
  <p><strong>UPDATE:</strong> I don't think my reading of the comparator order is wrong. Look the answer given in the following thread: <a href="https://stackoverflow.com/questions/870045/java-imap-fetch-messages-since-a-date">java imap fetch messages since a date</a></p>
  
  <p>Suppose we use that answer, and we have our boundaries from 7/15/2011 to 9/15/2011. Then we have the search compare a message with a given date of 8/4/2011. Then we would have the following:</p>
  
  <pre><code>                                              8/4/2011  &lt;     9/15/2011
  SearchTerm olderThen = new ReceivedDateTerm(ComparisonTerm.LT, someFutureDate);
  SearchTerm newerThen = new ReceivedDateTerm(ComparisonTerm.GT, somePastDate);
                                                8/4/2011  &gt;     7/15/2011
  </code></pre>
  
  <p>This would evaluate to true in both cases, which is what we expect, since the date is in the desired range. </p>
  
  <p>Likewise, my code is as follows:</p>
  
  <pre><code>    SearchTerm msgsOlderThanOETFirst =    8/4/2011  &lt; 8/5/2011
              new ReceivedDateTerm(DateTerm.LT, receivedDate); --&gt; TRUE
      SearchTerm msgsNewerThanFirst =       8/4/2011  &gt; 8/5/2011
              new ReceivedDateTerm(DateTerm.GT, receivedDate); --&gt; FALSE
  </code></pre>
  
  <p>The above evaluations to TRUE and FALSE are what I am expecting, and will receive. However, If we take a given message at 8/5/2011 12:00:00, we get the following:</p>
  
  <pre><code>    SearchTerm msgsOlderThanOETFirst =    8/5/2011 12:00:00  &lt; 8/5/2011 16:46:00
              new ReceivedDateTerm(DateTerm.LT, receivedDate); --&gt; TRUE?
      SearchTerm msgsNewerThanFirst =       8/4/2011 12:00:00  &gt; 8/5/2011 16:46:00
              new ReceivedDateTerm(DateTerm.GT, receivedDate); --&gt; FALSE?
  </code></pre>
  
  <p>Except, no -- we don't get that -- instead, we get the reverse..... it's easy to think yourself in circles at this point, but... I've double-checked this several times. Is there something wrong with JavaMail, or am I utterly confused? Please correct my confustion if it is the latter!</p>