Search code examples
javaandroidtimecountdowntimermilliseconds

difference between countdown timer time with actual time in Android?


I have one countdown timer in my activity. In which I have converted Mins into Millisecond.

Based on that Millisecond, I have managed to counter HH: mm: ss using CountDownTimer.

So, I want a difference between main time and remaining time.

For example, My main time is 00:10:00 and when I stop counter suppose my remaining time is 00:07:30. means I spend 2:30 seconds

An expected result I want is 150seconds.

Example Code:

public class MainActivity extends AppCompatActivity {

int duration;
TextView textView;
ProgressBar progressbar;
Button btn_stop, btn_resume;
CountDownTimer counter;
int i = 1;
int remainingDuration;
int countDownInterval = 1000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.tvSampleText);
    progressbar = (ProgressBar) findViewById(R.id.progressbar);
    btn_stop = (Button) findViewById(R.id.btn_stop);
    btn_resume = (Button) findViewById(R.id.btn_resume);

    // duration=81200000; //6 hours
    duration = (int) TimeUnit.MINUTES.toMillis(1);
    counter = new MyCount(duration, countDownInterval);
    counter.start();

    btn_stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            counter.cancel();
        }
    });

    btn_resume.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            counter = new MyCount(remainingDuration, countDownInterval);
            counter.start();
        }
    });
}

public class MyCount extends CountDownTimer {
    public MyCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onFinish() {
        textView.setText("00:00:00");
    }

    @Override
    public void onTick(long millisUntilFinished) {
        i++;
        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;

        long elapsedHours = millisUntilFinished / hoursInMilli;
        millisUntilFinished = millisUntilFinished % hoursInMilli;

        long elapsedMinutes = millisUntilFinished / minutesInMilli;
        millisUntilFinished = millisUntilFinished % minutesInMilli;

        long elapsedSeconds = millisUntilFinished / secondsInMilli;

        String yy = String.format("%02d:%02d:%02d", elapsedHours, elapsedMinutes, elapsedSeconds);
        textView.setText(yy);

        progressbar.setProgress((int) i * 100 / (duration / (int) secondsInMilli));
        remainingDuration = (int) millisUntilFinished;
    }
  }
}

Solution

  • First convert the minutes duration into seconds

    long secs = minutes * 60; 10 * 60 = 600
    

    and inside on tick, you get the remaining time in milliseconds so keep the value as global reference and convert it into seconds as

    long mMillisUntilFinished;
    
    
    void onTick (long millisUntilFinished){
        // mMillisUntilFinished = 450000
        mMillisUntilFinished = millisUntilFinished;
    }
    

    then when you press stop then get the renaming time difference as


    long diffSeconds = secs - (mMillisUntilFinished / 1000)
    //                 600  - (450000 /1000)
    //                 600  - 450
    //                 150 
    

    update: As suggested, calculate the seconds for both duration and left time then subtract them to get the result. Use long instead of int casting

    @Override
    public void onTick(long millisUntilFinished) {
        i++;
        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;
        long tempMili = millisUntilFinished;
    
        long elapsedHours = tempMili / hoursInMilli;
        tempMili = tempMili % hoursInMilli;
    
        long elapsedMinutes = tempMili / minutesInMilli;
        tempMili = tempMili % minutesInMilli;
    
        long elapsedSeconds = tempMili / secondsInMilli;
    
        String yy = String.format("%02d:%02d:%02d", elapsedHours, elapsedMinutes, elapsedSeconds);
        textView.setText(yy);
    
        progressbar.setProgress((int) i * 100 / (duration / (int) secondsInMilli));
        remainingDuration = (duration/ 1000) - (millisUntilFinished/1000);
    }