title: how to calculate age or difference between two dates in java
body: <p>I want to get the number of months between 2 dates.The dates are someones birthday and the current date.So i m getting the number of years in between the two dates but not the number of months..</p>
  
  <p>Suppose my dates are <strong>06/09/2011</strong> and <strong>06/11/2012</strong>.So i want the answer to be 1yr 2mths .I am getting the year but not the month.Please help.Below is the code for getting the number of years       </p>
  
  <pre><code> public int getAge(Date dateOfBirth)      {                                                                                                                                                                         
  
      today = Calendar.getInstance(); 
      Calendar birthDate = Calendar.getInstance();
  
      birthDate.setTime(dateOfBirth);
      if (birthDate.after(today)) {
          throw new IllegalArgumentException("Can't be born in the future");
      }
  
      age = today.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR);
      month = today.get(Calendar.MONTH) - birthDate.get(Calendar.MONTH);
  
      if ( (birthDate.get(Calendar.DAY_OF_YEAR) - today.get(Calendar.DAY_OF_YEAR) &gt; 3) ||
              (birthDate.get(Calendar.MONTH) &gt; today.get(Calendar.MONTH ))){
          days = birthDate.get(Calendar.DAY_OF_MONTH) - today.get(Calendar.DAY_OF_MONTH);
          age--;
  
          Toast.makeText(getApplicationContext(), "inside if", Toast.LENGTH_SHORT).show();
          Log.e("month is",month+"");
          Log.e("Days",days+ " left");
  
  
      }else if ((birthDate.get(Calendar.MONTH) == today.get(Calendar.MONTH )) &amp;&amp;
                (birthDate.get(Calendar.DAY_OF_MONTH) &gt; today.get(Calendar.DAY_OF_MONTH ))){
          Toast.makeText(getApplicationContext(), "inside else if", Toast.LENGTH_SHORT).show();
  
          age--;
      }
  
      return age;
  </code></pre>