title: Converting number of days(int) into days,months and years in java (including leap year)
body: <ol>
  <li>I used this code to convert number of days into respecting days,months and year</li>
  <li>But the result are not precise because i don't take account for leap year and month    with 31 days </li>
  <li>What is the best way to solve/encounter this issue</li>
  <li>field_Date1 and field_Date2 are input from my program</li>
  <li>duration=field_Date2-field_Date1</li>
  <li>duration is the number of days(int b)</li>
  <li><p>In the if else i do the conversion(but the condition are not precise)</p>
  
  <pre><code>import java.util.concurrent.TimeUnit;
  import java.math.RoundingMode;
  import java.text.DecimalFormat;
  
  if (field_Date1 == null || field_Date2 == null){
      return "";
  } else {
      Date startDate = (Date)field_Date1;
      Date endDate = (Date)field_Date2;
      long duration  = endDate.getTime() - startDate.getTime();
      long diffInDays = TimeUnit.MILLISECONDS.toDays(duration);
      long diff = duration - TimeUnit.DAYS.toMillis(diffInDays);
      double diffToHours = TimeUnit.MILLISECONDS.toHours(diff);
      float hoursToDay = (float) (diffToHours / 24.0);
      float a =hoursToDay+diffInDays;
      a=Math.floor(a)
      int b = (int)a
  
      if(b&lt;30)
      {
          StringBuilder sb = new StringBuilder("Day: ")
          sb.append(b)
          String c = sb.toString()
          c
      }
  
      else if(b&lt;366)
      {
          int months = b/30
          int days_out=b%30
  
          StringBuilder p1 = new StringBuilder("Days: ")
          StringBuilder p2 = new StringBuilder("Months: ")
          StringBuilder p3 = new StringBuilder("     ")
  
  
          p1.append(days_out)
          p2.append(months)
          p2.append(p3)
          p2.append(p1)
  
          String c=p2.toString()
          c
      }
  
      else
      {
          StringBuilder p1 = new StringBuilder("Months: ")
          StringBuilder p2 = new StringBuilder("Years: ")
          StringBuilder p3 = new StringBuilder("     ")
          StringBuilder p4 = new StringBuilder("Days: ")
  
          int years = b/365
          int days_out=b%365
  
          if(days_out&gt;30)
          {
              int m1 = days_out/30
              int m2 = days_out%30
  
              p2.append(years)
              p1.append(m1)
              p4.append(m2)
              p2.append(p3)
              p2.append(p1)
              p2.append(p3)
              p2.append(p4)
  
              String hj = p2.toString()
              return hj
          }
  
          else
          {
          p4.append(days_out)
          p2.append(years)
          p2.append(p3)
          p2.append(p4)
  
          String c=p2.toString()
          return c
          }
  
      }
  }
  </code></pre></li>
  </ol>