2014-02-10 208 views
0

我的持续时间目前以双精度格式以秒为单位。我如何转换它显示,多少小时,分钟和秒。如果我除以60就会有余数。另外,我如何将我的double转换为Integer?如果我只是初始化我作为整数,就会有一个错误:以小时,分钟,秒钟(以秒为单位)获取时间

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

* EDIT(对不起,我写了错误的输出):·让说,秒为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; 
    } 
}} 
+0

您是否在寻找这双d = 5.555d; int n =(int)d; – Rembo

回答

3

遵循此代码段

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

是我已经计算过的秒数“timeInMillis”了吗?因为我计算的时间是几秒钟,我怎么转换为毫秒?答案也遵循格式(请参阅更新) – Myst

+0

乘以1000以将秒转换为毫秒(timeInSec * 1000)。 –

+0

谢谢!它确实有用。 68秒变成1分8秒。非常感谢。但我把时间放在两倍,因为涉及它的其他变量似乎也是双倍。你知道我如何将double转换为int。我只需要将它转换一次再单独传递。我使用parstInt,但最后一次我使用它不能在双倍的工作。没有在问题中说明,但感谢如果你可以帮助 – Myst

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); 
} 
相关问题