CountDown计时器未显示剩余天数

时间:2014-08-01 10:00:43

标签: android datetime timer countdown countdowntimer

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

获得这个:

       Time remaining: 630 hours, 57 minutes, 25 seconds

需要这个:

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

活动代码: -

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

4 个答案:

答案 0 :(得分:1)

编辑:

我正在编辑这个以强调为什么让Jodatime做重物可能是一个更好的选择。你可以完全删除ToReadableString,你需要的只是(我将一些变量拉到常量中,这不是绝对必要的):

private static final PeriodType PERIOD_TYPE = PeriodType.dayTime().withMillisRemoved();
private static final PeriodFormatter PERIOD_FORMATTER = PeriodFormat.getDefault();

public void onTick(long millisUntilFinished) {
    Period period = new Period(millisUntilFinished, PERIOD_TYPE);
    countdown.setText("Time remaining: " + PERIOD_FORMATTER.print(period));
}

在最上面,你可以轻松获得Jodatime的所有好处。更少的代码,更少的错误。

编辑结束;

Joda时间的public Period(long duration)构造函数使用标准字段集,因此不包括年/日等,您应该使用替代构造函数public Period(long duration, PeriodType type)

为您的示例替换:

String result = ToReadableString(new org.joda.time.Period(millisUntilFinished));

使用:

String result = ToReadableString(new org.joda.time.Period(millisUntilFinished, org.joda.time.PeriodType.dayTime().withMillisRemoved()));

作为旁注,Jodatime可以将痛苦从一段时间转换为字符串。请使用PeriodFormatter。 e.g:

String yourOutputString = PeriodFormat.getDefault().print(period);

您可能希望修复导入以提高可读性。

Fyr:Joda time Javadoc

答案 1 :(得分:1)

在ToReadableString函数中,只需执行以下操作即可获得从小时开始的天数 - 您还必须解决其他差异:

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


//rest of conversion code

答案 2 :(得分:0)

您可以手动计算。 我在下面粘贴方法,用于从MilliSecond的差值计算小时和秒的日期。

public static String getDateHourMinSecond(long diffMillis) {

    long days = TimeUnit.MILLISECONDS.toDays(diffMillis);
    diffMillis -= TimeUnit.DAYS.toMillis(days);
    long hours = TimeUnit.MILLISECONDS.toHours(diffMillis);
    diffMillis -= TimeUnit.HOURS.toMillis(hours);
    long minutes = TimeUnit.MILLISECONDS.toMinutes(diffMillis);
    diffMillis -= TimeUnit.MINUTES.toMillis(minutes);
    long seconds = TimeUnit.MILLISECONDS.toSeconds(diffMillis);
    String dhms = days + " : " + hours + " : " + minutes + " : " + seconds;
    return dhms;
}

答案 3 :(得分:0)

尝试此功能

public void clockCounter(Date startTime) {
Date currentDate =null;
SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");

            Calendar cal = Calendar.getInstance();
            currentDate = dateFormat.parse(dateFormat.format(cal.getTime()));

        long diffInSec = startTime.getTime() - currentDate.getTime();

        long seconds = diffInSec % 60;
        diffInSec /= 60;
        long minutes = diffInSec % 60;
        diffInSec /= 60;
        long hours = diffInSec % 24;
        diffInSec /= 24;
        long days = diffInSec;

        Log.i("Difference", days+":"+hours+":"+minutes+":"+seconds);
    }