安卓的倒计时显示
项目需要没找到太适合的,自己简单写了下

效果.png
这里需要一个结束时间yyyy-MM-dd,显示的当前时间距离结束时间剩余的时间
private String ShengYuShiJian(String endTime){
Date nowDate = new Date(System.currentTimeMillis());//当前时间
long nowDateLong = nowDate.getTime();
String endTimeStr = endTime+ " 00:00:00";
// String endTimeStr = "2017-12-29"+ " 15:45:00";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date enddate = null;
try {
enddate = simpleDateFormat.parse(endTimeStr);
} catch (ParseException e) {
e.printStackTrace();
}
long enddateLong = enddate.getTime();//结束时间
long timeLong = enddateLong - nowDateLong;//剩余时间
if(timeLong <= 0 ){
return "计时结束";
}else {
if (timeLong<60*1000)
return timeLong/1000 + "秒";
else if (timeLong<60*60*1000){
Long timeLongM = timeLong/(1000*60);
Long timeLongS = timeLong%(1000*60);
return timeLongM + "分"+timeLongS/1000 + "秒";
}
else if (timeLong<60*60*24*1000){
Long timeLongH = timeLong/(1000*60*60);
Long timeLongM = timeLong%(1000*60*60);
Long timeLongS = timeLong%(1000*60);
return timeLongH+"小时"+ timeLongM/(1000*60) + "分"+timeLongS/1000 + "秒";
}
else { //(timeLong<60*60*24*1000*7)
Long timeLongD = timeLong/(1000*60*60*24);
Long timeLongH = timeLong%(1000*60*60*24);
Long timeLongM = timeLong%(1000*60*60);
Long timeLongS = timeLong%(1000*60);
return timeLongD + "天"+timeLongH/(1000*60*60)+"小时"+ timeLongM/(1000*60) + "分"+timeLongS/1000 + "秒";
}
}
}
在onCreate中执行upDateUI
private void upDateUI() {
/**
* CountDownTimer 实现倒计时
*/
CountDownTimer countDownTimer = new CountDownTimer(1000000000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTextViewTime.setText(ShengYuShiJian(mProduct.getJxBeginDate()));
}
@Override
public void onFinish() {
}
}; //调用 CountDownTimer 对象的 start() 方法开始倒计时,也不涉及到线程处理
countDownTimer.start();
}
这里的millisInFuture随便写的,有点长。。一般人也不会盯着看这么长的倒计时