android - 倒数计时器不显示剩余天数

标签 android datetime timer countdown countdowntimer

我正在编写一个程序,其中我使用倒计时直到特定日期和时间,结果我得到以小时、分钟、秒格式倒计时,但我还想显示天数

得到这个:

       Time remaining: 630 hours, 57 minutes, 25 seconds

需要这个:

       Time remaining: 26 days, 11 hours, 57 minutes, 25 seconds

Activity 代码:-

    public void onTick(long millisUntilFinished) {
                String result = ToReadableString(new org.joda.time.Period(millisUntilFinished));                   
                countdown.setText("Time remaining: " + result);
            }

    private String ToReadableString(Period period) {

        int days = period.get(DurationFieldType.days());

        int hours = period.get(DurationFieldType.hours());
        int minutes = period.getMinutes();
        int seconds = period.getSeconds();

        StringBuilder sb = new StringBuilder();

        if (days > 0) {
            sb.append(days);
            if (days == 1) {
                sb.append(" day, ");
            } else {
                sb.append(" days, ");
            }
        }

        if (hours > 0) {
            sb.append(hours);
            if (hours == 1) {
                sb.append(" hour, ");
            } else {
                sb.append(" hours, ");
            }
        }

        if (minutes > 0) {
            sb.append(minutes);
            if (minutes == 1) {
                sb.append(" minute, ");
            } else {
                sb.append(" minutes, ");
            }
        }

        if (seconds > 0) {
            sb.append(seconds);
            if (seconds == 1) {
                sb.append(" second, ");
            } else {
                sb.append(" seconds, ");
            }
        }

        String result = sb.toString().trim();

        if (result.endsWith(","))
            result = result.substring(0, result.length() - 1);

        return result;
    }
}

最佳答案

在您的 ToReadableString 函数中,只需执行以下操作即可从小时数中获取天数 - 您还必须计算出其他差异:

int days = hours/24;
hours = hours % 24;


//rest of conversion code

关于android - 倒数计时器不显示剩余天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25077640/

相关文章:

javascript - window.onload 在 Android WebView 中不起作用

python - 在 Pandas 中结合 CustomBusinessDay 和 BusinessHour 类

javascript - 使用 javascript 或 jQuery 将小时和分钟转换为毫秒

python - 创建自定义日期范围,一天 22 小时 python

asynchronous - 如何在 flutter 应用程序中显示 5 分钟不可停止的计时器?

android - 如何为 Google Play 生成 "Keystore"?

android - Robolectric 3.0 不适用于 AppCompat 21+

android - 在适配器中以编程方式刷新 ListView

ajax - JS 2分钟倒计时然后执行功能?

java - sleep 暂停整个线程