title: How to fix error with CountDownTimer in android app
body: <p>I'm trying to make a countdown in days, hours, minutes, seconds from a starting date and I'm getting the days wrong for some reason I cannot find.</p>
  
  <pre><code>    String givenDateString = "2019-05-15T09:00:00";
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
      try {
          Date mDate = sdf.parse(givenDateString);
           timeInMilliseconds = mDate.getTime();
      } catch (ParseException e) {
          e.printStackTrace();
      }
  
      new CountDownTimer(timeInMilliseconds, 1000) {
          @Override
          public void onTick(long millisUntilFinished) {
              long day = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
              millisUntilFinished -= TimeUnit.DAYS.toMillis(day);
  
              long hour = TimeUnit.MILLISECONDS.toHours(millisUntilFinished);
              millisUntilFinished -= TimeUnit.HOURS.toMillis(hour);
  
              long minute = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
              millisUntilFinished -= TimeUnit.MINUTES.toMillis(minute);
  
              long second = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished);
  
              prueba.setText("Days: "+day+" Hours: "+hour+" Minutes: "+minute+" Seconds: "+second);
          }
  
          @Override
          public void onFinish() {
              // What ever you want !
          }
  
      }.start();
  </code></pre>
  
  <p>And I'm getting this result:</p>
  
  <p><a href="https://i.stack.imgur.com/g0yEW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/g0yEW.png" alt="enter image description here"></a></p>
  
  <p>New error android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.</p>
  
  <pre><code>TextView prueba;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      prueba = findViewById(R.id.prueba);
  
      Timer timer = new Timer();
      timer.schedule(new TimerTask() {
          @Override
          public void run() {
              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
              Date currentDate = null;
              Date destinationDate = null;
              try {
                  currentDate = Calendar.getInstance().getTime();
                  destinationDate = sdf.parse("2019-05-15T09:00:00");
  
              } catch (ParseException e) {
                  e.printStackTrace();
              }
              calculateDifference(currentDate, destinationDate);
          }
      }, 0, 1000);//Update text every second
  }
  
  public void calculateDifference(Date startDate, Date endDate) {
      long different = endDate.getTime() - startDate.getTime();
  
      long secondsInMilli = 1000;
      long minutesInMilli = secondsInMilli * 60;
      long hoursInMilli = minutesInMilli * 60;
      long daysInMilli = hoursInMilli * 24;
  
      long days = different / daysInMilli;
      different = different % daysInMilli;
  
      long hours = different / hoursInMilli;
      different = different % hoursInMilli;
  
      long minutes = different / minutesInMilli;
      different = different % minutesInMilli;
  
      long seconds = different / secondsInMilli;
  
      Log.e("calculation", "Days: " + days + " Hours: " + hours + " Minutes: " + minutes + " Seconds: " + seconds);
      prueba.setText("Days: " + days + " Hours: " + hours + " Minutes: " + minutes + " Seconds: " + seconds);
  }
  </code></pre>