Java Date after before time not working as expected in production
<p>Here is my code:</p>

<pre><code>long treatmentTimeBeginDay;
if ( effectiveBegin.after(businessClosing) ) {
    LOGGER.debug("Compute treatment time for beginDay = 0: the effective begin {} is after business closing {}",
                            config.formatLogDate(effectiveBegin),config.formatLogDate(businessClosing));
    treatmentTimeBeginDay = 0;
} else {
    LOGGER.debug("Compute treatment time for beginDay between {} and {}",config.formatLogDate(effectiveBegin),config.formatLogDate(businessClosing));
    treatmentTimeBeginDay = businessClosing.getTime() - effectiveBegin.getTime();
}
Preconditions.checkState( treatmentTimeBeginDay &gt;= 0 , "Internal bug! treatmentTimeBeginDay="+treatmentTimeBeginDay );
</code></pre>

<p>effectiveBegin and businessClosing are not null, also checked by Guava preconditions and you can see it in the logs...</p>

<p>It runs fine in most cases but in production we have these errors:</p>

<blockquote>
  <p>Caused by: java.lang.IllegalStateException: Internal bug!
  treatmentTimeBeginDay=-852</p>
</blockquote>

<p>I don't give you the rest of the stack/code because it should be enough...
The exception is explicitly raised by my Guava checkState call.</p>

<p>I also have the log:</p>

<blockquote>
  <p>DEBUG [BusinessHoursUtils.java:257] llairie - Compute treatment time
  for beginDay between 7/19/12 8:00 PM and 7/19/12 8:00 PM</p>
</blockquote>

<p>(I can't have log with millies for now)</p>

<hr>

<p>What i'm trying to understand is.</p>

<p>If i get the log i gave you, this means that the test <code>if ( effectiveBegin.after(businessClosing) )</code> is false, so effectiveBegin should be before or equals to the businessClosing.</p>

<p>In this case effectiveBegin timestamp should be lower than the businessClosing timestamp.</p>

<p>So when i do <code>businessClosing.getTime() - effectiveBegin.getTime();</code> i would expect to have a positive number.</p>

<p>So please can someone tell me why i have -852 milliseconds in my exception message? How is this possible?</p>

<hr>

<p><strong>Edit:</strong> I was suspecting a tricky case where the after/before method wouldn't work for milliseconds and it seems that's the problem since i could reproduce it locally.</p>

<p>The 2 dates at runtime are:</p>

<pre><code>businessClosing = {java.util.Date@503}"Thu Jul 19 20:00:00 CEST 2012"
fastTime = 1342720800000
cdate = null

effectiveBegin = {java.sql.Timestamp@498}"2012-07-19 20:00:00.999"
nanos = 999000000
fastTime = 1342720800000
cdate = {sun.util.calendar.Gregorian$Date@512}"2012-07-19T20:00:00.000+0200"
</code></pre>

<p>With these runtime objects, <code>effectiveBegin.after(businessClosing) = false</code>
If i set in DB effectiveBegin = 2012-07-19 20:00:01.000, 1 millisecond later, then the test = true</p>

<p>In both cases i would expect to have <code>effectiveBegin.after(businessClosing) = true</code></p>

<p>It seems, like ametren suspected, that my dates are different.</p>

<p>So in the end, what's the problem? 
Aren't we supposed to be able to compare the 2 date instance with a millisecond precision? 
Even if they are subclasses of the java.util.Date?</p>
