淘寶搶購倒計時(時、分、秒)
@Override
public void run() {
toDataCount();
}
};
private void toDataCount() {
SimpleDateFormat df = new SimpleDateFormat("MM-dd HH:mm:ss");
try {
String date = df.format(new Date());
Date d2 = df.parse(date);//系統時間
Date d1 = df.parse(AppConfig.getTmLongToStr_(gameInfo.getStartDateUnix() * 1000));//伺服器獲取時間
long diff = d1.getTime() - d2.getTime();//這樣得到的差值是微秒級別
long days = diff / (1000 * 60 * 60 * 24);
long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
long second = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;
if (days <= 0 && hours <= 0 && minutes <= 0 & second <= 0) {
time_tv.setText(hours + ":" + minutes + ":" + second);
handler.removeCallbacks(timeRunnable);
} else {
time_tv.setText(hours + ":" + minutes + ":" + second);
handler.postDelayed(timeRunnable, 1000);
}
} catch (Exception e) {
}
}
handler.post(timeRunnable);//呼叫倒計時