我写了一个倒数计时器,我为此设定了时间,当我显示其设定的小时数+1时,我该如何纠正它呢?
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
future=dateFormat.parse("2018-09-25 15:00:00");
Date now=new Date();
if (!now.after(future)) {
long diff = future.getTime() - now.getTime();
long days = diff / (24 * 60 * 60 * 1000);
diff -= days * (24 * 60 * 60 * 1000);
long hours = diff / (60 * 60 * 1000);
diff -= hours *( 60 * 60 * 1000);
}
...... 例如,如果应该从1day 13:00:00开始计数,则从14开始计数
答案 0 :(得分:0)
希望这可能对您有所帮助。所有您要做的将时间转换为毫。 而您所要做的就是使用计时器。更新您在标签中剩余的时间。 这是将dateTimet转换为millis的示例代码。
String myDate = "2014/10/29 18:10:45";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = sdf.parse(myDate);
long millis = date.getTime();
CountDownTimer countDownTimer = new CountDownTimer(millis , 1000) {
@Override
public void onTick(long millisUntilFinished) {
if (getActivity() != null && !getActivity().isFinishing()) {
expiryLabel.setText(getResources().getString(R.string.expiresIn,
" " +formatTime(millisUntilFinished));
}
}
@Override
public void onFinish() {
if (getActivity() != null && !getActivity().isFinishing()) {
// do what ever you want
}
}
};
countDownTimer.start();
}
public static String formatTime(long millis) {
String output = "00:00:00";
long seconds = millis / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
seconds = seconds % 60;
minutes = minutes % 60;
hours = hours % 60;
String secondsD = String.valueOf(seconds);
String minutesD = String.valueOf(minutes);
String hoursD = String.valueOf(hours);
if (seconds < 10)
secondsD = "0" + seconds;
if (minutes < 10)
minutesD = "0" + minutes;
if (hours < 10)
hoursD = "0" + hours;
output = hoursD + ":" + minutesD + ":" + secondsD;
return output;
}