【问题标题】:Converting seconds to hours, minutes and seconds将秒转换为小时、分钟和秒
【发布时间】:2015-06-02 06:24:13
【问题描述】:

我想在 android 中长时间使用倒数计时器...

目前,我正在使用此代码,但在几个小时后,比如 10 小时后,格式变为 10 : 650 :56 (hh:mm:ss)... 对于较短的时间,它可以完美运行...

 private Runnable updateTimerMethod = new Runnable() {

    public void run() {
        timeInMillies = SystemClock.uptimeMillis() - startTime;

        finalTime = timeSwap + timeInMillies;

        int seconds = (int) (finalTime / 1000);
        int minutes = seconds / 60;
        int hours = minutes / 60;
        seconds = seconds % 60;
        int milliseconds = (int) (finalTime % 1000);

        String timer = ("" + String.format("%02d", hours) + " :  "
                + String.format("%02d", minutes) + " : "
                + String.format("%02d", seconds));

        myHandler.postDelayed(this, 0);

        sendLocalBroadcast(timer);

    }

};

【问题讨论】:

  • 你有预付款吗?

标签: java android string time timer


【解决方案1】:

您的分钟代码几乎是正确的,但您必须将其模数为 60,就像您对秒所做的一样。否则,您的价值仍将包括所有时间。

【讨论】:

    【解决方案2】:

    使用此功能:

    private static String timeConversion(int totalSeconds) {
    
        final int MINUTES_IN_AN_HOUR = 60;
        final int SECONDS_IN_A_MINUTE = 60;
    
        int seconds = totalSeconds % SECONDS_IN_A_MINUTE;
        int totalMinutes = totalSeconds / SECONDS_IN_A_MINUTE;
        int minutes = totalMinutes % MINUTES_IN_AN_HOUR;
        int hours = totalMinutes / MINUTES_IN_AN_HOUR;
    
        return hours + " : " + minutes + " : " + seconds;
    }
    

    您可以在以下位置找到其他解决方案:

    https://codereview.stackexchange.com/q/62713/69166

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 2012-01-06
      • 2013-05-05
      相关资源
      最近更新 更多