title: Use compareTo for year,month and day.. Java
body: <p>I wanna sort the dates out on the screen but the thing is, when I use <code>compareTo</code> method, I can only use it with one of the Date attributes: Year, month or day.. not all of them. This is a custom class, not <code>java.util.Date</code> class.</p>
  
  <p>I am required to sort it out on the screen using <code>compareTo</code> method. </p>
  
  <p>This is what I have done.</p>
  
  <pre><code>public int compareTo(Date date) {
      if(getYear()-date.getYear()&gt;0 &amp;&amp; getMonth()-date.getMonth()&gt;0){
  
          return 1;
      }
      if(getYear()-date.getYear()&lt;0 &amp;&amp; getMonth()-date.getMonth()&lt;0 ){
  
          return -1;
      }
      return 0;
  }
  </code></pre>