我正在尝试查找当前时间值与 HH:MM:SS 格式的 future 时间之间的差异。
例如:
当 date1 为“2017-05-11T20:30”且 date2 为“2017-05-11T21:40”时,输出应为 01:10:00。
这是我正在尝试的代码,其中我试图找到当前时间和 future 时间值之间的差异:
public void updateTimeRemaining() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
String currentTime = simpleDateFormat.format(new Date());
long difference = simpleDateFormat.parse(endTime).getTime() - simpleDateFormat.parse(currentTime).getTime();
if (difference>0) {
String hms = String.format("%02d:%02d:%02d", millisLeft/(3600*1000),
millisLeft/(60*1000) % 60,
millisLeft/1000 % 60);
textView.setText(hms); //setting the remaining time in a textView
}
}
我每秒调用方法updateTimeRemaining()
,以便 TextView 像计时器一样每秒更新。我面临的问题是秒值总是返回 0。相反,我希望秒值每秒更新一次,如下所示:
01:50:45
01:50:44
01:50:43
01:50:42...
最佳答案
你可以使用
difference = simpleDateFormat.parse(endTime).getTime() - new Date().getTime();
代替这些代码行:
String currentTime = simpleDateFormat.format(new Date());
long difference = simpleDateFormat.parse(endTime).getTime() - simpleDateFormat.parse(currentTime).getTime();
这应该可以正常工作。
关于java - 求两个时间值之间的时间差始终给出 "Second"值 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43920007/