title: Java Long Comparison
body: <p><strong>Introduction</strong>: I have a method in Android for doing Long comparisons. Basically, I compare dates (timestamps/epochs). One of these comparisons checks if dates are equal "if (a2 == a1 &amp;&amp; b2 == b1)".</p>
  
  <p><strong>Problem</strong>: The problem is that even when these numbers are equal, the "if" does not work. I am also attaching a screenshot from the debugging screen with the values that are being compared in an example.</p>
  
  <p><strong>Question</strong>: Would you know why the condition is not working? I can't figure since it is just math that should work. Maybe there is a casting issue that I am not seeing? All other cases the "if" works.</p>
  
  <p><strong>Method</strong>:</p>
  
  <pre><code>public ArrayList&lt;Integer&gt; getConflicts(ArrayList&lt;AssetBooking&gt; assetBookings) {
  
          Epoch epoch = new Epoch();
          ArrayList&lt;Integer&gt; conflicts = new ArrayList&lt;&gt;();
  
          for (AssetBooking aB : assetBookings) {
  
              Long a1 = aB.getFromDatetime() / 1000;
              Long b1 = aB.getToDatetime() / 1000;
  
              Long a2 = epoch.getEpochNoSeconds(fromDatetime);
              Long b2 = epoch.getEpochNoSeconds(toDatetime);
  
              if (a2 == a1 &amp;&amp; b2 == b1) {
                  System.out.println("equals");
              }
  
              if ((a2 &lt; a1 &amp;&amp; b2 &gt;= a1) || (a2 == a1) || (a2 &gt; a1 &amp;&amp; b2 &lt; b1) || (a2 &gt; a1 &amp;&amp; a2 &lt; b1) || (a2 == a1 &amp;&amp; b2 == b1)) {
                  conflicts.add(assetBookings.indexOf(aB));
              }
  
          }
  
          return conflicts;
  
      }
  </code></pre>
  
  <p><strong>Screenshot</strong>:
  <a href="https://i.stack.imgur.com/8ZT5z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8ZT5z.png" alt="enter image description here"></a></p>