我正在编写一个程序,其中我使用倒计时直到特定日期和时间,结果我得到以小时、分钟、秒格式倒计时,但我还想显示天数
得到这个:
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/