以小时,分钟,秒为单位获得时间(目前以秒为单位)

时间:2014-02-10 04:13:10

标签: android time

我的持续时间目前是双倍格式的秒数。我怎么转换它做显示,多少小时,分钟和秒。如果我除以60就会有余数。另外,我如何将我的双重转换为整数?如果我只是将我的时间初始化为整数,则会出错:

  

“类型不匹配:无法从double转换为int”`at“time = n * 30 + r1;”

* 编辑(对不起,我写错了输出): *让我们说秒是132秒。我希望在0小时,2分12秒

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;
    }
}}

2 个答案:

答案 0 :(得分:4)

关闭此代码段

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;
    }

答案 1 :(得分:0)

检查我的回答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);
}