title: D-Day Counter Java
body: <p>I am trying to develop an android app that displays how many years, months, and days left for the future date specified. However, I am getting wield values :/</p>
  
  <p>Here, the remaining year, month, and days until 2018/06/18 are calculated: calculating remaining months were quite difficulty since each month can have different number of days. So I believe I made an mistake while trying to obtaining remaining months. However, my result was frustrating: I got 41 years, 01 months, negative wield value days... </p>
  
  <pre><code>public void countDownStart() {
      handler = new Handler();
      runnable = new Runnable() {
          @Override
          public void run() {
              handler.postDelayed(this, 1000);
              long diff;
              long years=0;
              long months=0;
              long  days=0;
              long temp_days=0;
              int current_month;
  
              Calendar futureDate = Calendar.getInstance();
              futureDate.set(Calendar.DAY_OF_MONTH, 18);
              futureDate.set(Calendar.MONTH, 6);
              futureDate.set(Calendar.YEAR, 2018);
              Calendar referenceDate = Calendar.getInstance();
  
              Calendar currentDate = Calendar.getInstance();
  
              if (futureDate.after(currentDate)) {
                  diff = futureDate.getTimeInMillis() - currentDate.getTimeInMillis();
                  days = diff/(1000*60*60*24);
                  years = diff / (1000 * 60 * 60 * 24 * 365);
                  diff = diff - years * 1000 * 60 * 60 * 24 * 365;
                  months = 0;
                  boolean flag = true;
                  current_month = referenceDate.get(Calendar.MONTH);
                  while (flag) {
                      switch (current_month) {
                          case 0:
                          case 2:
                          case 4:
                          case 6:
                          case 7:
                          case 9:
                          case 11:
                              diff = diff - 1000 * 60 * 60 * 24 * 31;
                              break;
                          case 1:
                              diff = diff - 1000 * 60 * 60 * 24 * 28;
                              break;
                          case 3:
                          case 5:
                          case 8:
                          case 10:
                              diff = diff - 1000 * 60 * 60 * 24 * 31;
                              break;
                      }
                      months = months + 1;
                      referenceDate.add(Calendar.MONTH, 1);
                      current_month=referenceDate.get(Calendar.MONTH);
                      temp_days = diff / 1000 * 60 * 60 * 24;
  
                      if (temp_days &lt;= 31) {
                          int next_month = referenceDate.get(Calendar.MONTH);
                          int days_in_month;
  
                          if (next_month == 0 || next_month == 2 || next_month == 4 || next_month == 6 ||
                                  next_month == 7 || next_month == 9 || next_month == 11) {
                              days_in_month = 31;
                          } else if (next_month == 1) {
                              days_in_month = 28;
                          } else {
                              days_in_month = 30;
                          }
  
                          if (temp_days &lt; days_in_month) {
                              flag = false;
                              //days = temp_days;
                          }
                      }
                  }
  
  
              }
              else
              {
                  years=0;
                  months=0;
                  days=0;
              }
              txtTimerYear.setText(""+String.format("%02d",years));
              txtTimerMonth.setText(""+String.format("%02d",months));
              txtTimerDay.setText(""+String.format("%03d",days));
  
          }
      };
      handler.postDelayed(runnable,1*1000);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I would really appreciate your help!!
  Thanks!!</p>