0

My duration is currently in seconds in double format. how do i convert it do display, how many hours, min, and seconds. if i just divide by 60 there will be a remainder. also, how do i convert my double to Integer? If I just initializes my time as Integer, it will have a error:

"Type mismatch: cannot convert from double to int"` at "time=n*30+r1;"

*EDIT(sorry i written the wrong output):*lets say the seconds is 132 seconds. i want to be in 0 hours, 2 min 12 seconds

double time = 0;
double hourTime=0;
double minTime=0;

    btnStartMove.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) {

            startButtonClicked=true;
            startDistance=true;
            counter= new MyCount(30000,1000);
            counter.start();
            btnStartMove.setText("Started...");
        }
    });


//button to get duration
btnDuration.setOnClickListener(new OnClickListener() 
{
    @Override
    public void onClick(View v) {
        if(startButtonClicked=true) 
        {
            time=n*30+r1;
            velocity=dist/time;
            DecimalFormat df = new DecimalFormat("#.##");
            Toast.makeText(MainActivity.this,"Duration :"+String.valueOf(time) + "Speed :"+String.valueOf(df.format(velocity)),Toast.LENGTH_LONG).show();

        }       
   }
   });

    public class MyCount extends CountDownTimer{
    public MyCount(long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);
    }
    @Override
    public void onFinish() {
        counter= new MyCount(30000,1000);
     counter.start();
     n=n+1;
    }
    @Override
    public void onTick(long millisUntilFinished) {
        s1=millisUntilFinished;
        r1=(30000-s1)/1000;
    }
}}
Myst
  • 177
  • 1
  • 6
  • 24

2 Answers2

4

FOLLow this code snippet

private String calculateDifference(long timeInMillis){

        hours = (int) ((timeInMillis / (1000 * 60 * 60)));
        minutes = (int) ((timeInMillis / (1000 * 60)) % 60);
        seconds = (int) ((timeInMillis / 1000) % 60);
        return hours+":"+minutes+":"+seconds;
    }
Adhikari Bishwash
  • 2,770
  • 28
  • 32
  • is the "timeInMillis" my time in seconds that i already calculated? since my time i calculated is in seconds how do i converted to milli seconds? also does the answers follow the format(see update) – Myst Feb 10 '14 at 04:36
  • multiply by 1000 to convert sec to millis (timeInSec * 1000). – Adhikari Bishwash Feb 10 '14 at 04:39
  • thank you! it did works. 68 seconds becomes 1 min 8 seconds. thank you so much. but i treat the time in double as other variable involving it seem to be in double too.do you know how do i convert double to int. i just need to convert it one time before passing over separately. do i use parstInt, but last time i use that it does not work on double. not stated in the question, but appreciate if you could help – Myst Feb 10 '14 at 04:52
  • typecast double to int int i = (int)myDouble; – Adhikari Bishwash Feb 10 '14 at 04:57
  • thanks for the replies. i just initialize my new time to int at first, because i thought the method calculation cannot accept int, turn out it still works. – Myst Feb 10 '14 at 05:40
0

check my answer Timer display to countdown

@Override
public void onTick(long millisUntilFinished) {
 long temp_long = millisUntilFinished / 1000;

 second = temp_long % 60;
 hour = temp_long / 3600;
 minute = (temp_long / 60) % 60;
 String tempTimer;
 tempTimer = ((hour < 10) ? "0" + hour : "" + hour)+ ((minute < 10) ? ":0" + minute : ":"+ minute)+ ((second < 10) ? ":0" + second : ":" + second);
 mTextField.setText(tempTimer);
}
Community
  • 1
  • 1
PankajAndroid
  • 2,689
  • 3
  • 27
  • 41