我想运行一个倒计时,其中我想显示特定日期剩余的天数、小时数、秒数和毫秒数。并将不断变化,直到特定日期结束。 希望您能够理解。
提前致谢。
最佳答案
嗯,我认为问题是,你不知道如何利用时间。这里我有一个方法来计算我从数据库中解析的某些项目的时间量。 参数是一个 double 值,它以秒为单位获取整个时间。它返回一个字符串,其中包含以天、小时、分钟和秒为单位的时间。
public String timeCalculate(double ttime) {
long days, hours, minutes, seconds;
String daysT = "", restT = "";
days = (Math.round(ttime) / 86400);
hours = (Math.round(ttime) / 3600) - (days * 24);
minutes = (Math.round(ttime) / 60) - (days * 1440) - (hours * 60);
seconds = Math.round(ttime) % 60;
if(days==1) daysT = String.format("%d day ", days);
if(days>1) daysT = String.format("%d days ", days);
restT = String.format("%02d:%02d:%02d", hours, minutes, seconds);
return daysT + restT;
}
对于倒计时本身...将目标时间戳减去实际时间戳,瞧,您还剩下几秒:) 将这些秒放入此方法中,您就得到了剩余时间。现在你只需要做一些 UI 的事情;) 哦,对于通常的 Unix 时间戳,你可以使用这个小方法:
public static long getTimestamp() {
return System.currentTimeMillis() / 1000;
}
关于android - Android 中的倒计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3749297/