【问题标题】:get time in Hours, Min, Seconds (currently in seconds)以小时、分钟、秒为单位获取时间(当前以秒为单位)
【发布时间】:2014-02-10 12:44:56
【问题描述】:

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

“类型不匹配:无法从 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;
    }
}}

【问题讨论】:

  • 你在寻找这个双倍 d = 5.555d; int n = (int)d;

标签: android time


【解决方案1】:

FOLlow 这个代码 sn-p

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

【讨论】:

  • “timeInMillis”是我已经计算的秒数吗?因为我计算的时间是以秒为单位的,我如何转换为毫秒?答案是否也遵循格式(见更新)
  • 乘以 1000 将秒转换为毫秒 (timeInSec * 1000)。
  • 谢谢!它确实有效。 68 秒变成 1 分 8 秒。太感谢了。但我将时间视为双倍,因为涉及它的其他变量似乎也是双倍。你知道我如何将双精度转换为整数。我只需要在单独传递之前转换一次。我是否使用 parstInt,但上次我使用它在 double 上不起作用。问题中未说明,但如果您能提供帮助,我们将不胜感激
  • 将 double 类型转换为 int int i = (int)myDouble;
  • 感谢您的回复。一开始我只是将我的新时间初始化为int,因为我认为方法计算不能接受int,结果它仍然有效。
【解决方案2】:

查看我的答案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);
}

【讨论】:

    猜你喜欢
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 2011-10-25
    • 2017-02-20
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多