title: Use Calendar to get the number of days in February
body: <p>The question is to get day numbers of February of any year. My code is like this:</p>
  
  <pre><code>public static void main(String[] args) {
      System.out.println("2014-02 has " + getDaysOfFeb(2014) + "days");
      System.out.println("2016-02 has " + getDaysOfFeb(2016) + "days");
  }
  
  public static int getDaysOfFeb(int year) {
      Calendar c = Calendar.getInstance();
      // set year-01-31
      c.set(year, 0, 31);
      long lastDayofJan = c.getTimeInMillis();
  
      // set year-03-01
      c.set(year, 2, 1);
      long firstDayOfMar = c.getTimeInMillis();
  
      int date = (int) ((firstDayOfMar - lastDayofJan) / 1000 / 60 / 60 / 24);
  }
  </code></pre>
  
  <p>I got <code>Jan 31st</code> and <code>Mar 1st</code>, I use the difference of time to calculate the day numbers. But the result is:</p>
  
  <pre><code>2014-02 has 29days
  2016-02 has 30days
  </code></pre>
  
  <p>I don't understand why.</p>
  
  <p>When I do like this:</p>
  
  <pre><code>public static int getDaysOfFeb(int year) {
      Calendar c = Calendar.getInstance();
      // set year-01-31
      c.set(year, 2, 1);
      c.add(Calendar.DATE, -1); // last day of Feb
      int date = c.get(Calendar.DATE);
      return date;
  }
  </code></pre>
  
  <p>The result is right, as follow:</p>
  
  <pre><code>2014-02 has 28days
  2016-02 has 29days
  </code></pre>
  
  <p>Does anyone know what the difference is here?</p>