问题描述
我在弄清楚如何计算两个时间戳(小时,分钟,秒)之间经过的时间时遇到问题.结果是:
I have a problem figuring out how to calculate the elapsed time between two timestamps (hours, minutes, seconds). This is the result:
String starttime = beginElement.getTimestamp();
String endtime = element.getTimestamp();
I/开始时间:1496258892612
I/Start time: 1496258892612
我/结束时间:1496258928999
I/End time: 1496258928999
long diffTime = Long.parseLong(endtime) - Long.parseLong(starttime);
I/Diff time:36387
I/Diff time: 36387
String elapsedtime = new SimpleDateFormat("hh:mm:ss").format(Long.parseLong(String.valueOf(diffTime)));
我/经过时间:01:00:36
I/Elapsed time: 01:00:36
实际上,经过时间仅为36秒.它总是比结果少一小时.
In fact, the elapsed time is just 36 seconds. It's always one hour less than the result.
如何解决这个奇怪的问题?
How do I fix this strange issue?
推荐答案
您使用的API错误,解析不是您所需要的,因为时间与日期无关,而与持续时间有关....
you are using the API incorrectly, parse is not what you need since that time is not related to a date but to a duration period instead....
做类似的事情:
String periodAsHH_MM_SS = String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(diffTime),
TimeUnit.MILLISECONDS.toMinutes(diffTime) % TimeUnit.HOURS.toMinutes(1),
TimeUnit.MILLISECONDS.toSeconds(diffTime) % TimeUnit.MINUTES.toSeconds(1));
System.out.println("Duration in hh:mm:ss is: "+periodAsHH_MM_SS);
这篇关于Java/Android-计算时间戳之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!