应用程序,我需要计算两次之间的差异.我需要计算24小时的时差,以及两天的时间差(例如今天下午5点到明天上午9点).
我已经尝试了以下代码,计算仅适用于24小时的差异,
String dateStart = "08:00:00";
String dateStop = "13:00:00";
//HH converts hour in 24 hours format (0-23), day calculation
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date d1 = null;
Date d2 = null;
try
{
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
//in milliseconds
long diff = d2.getTime() - d1.getTime();
long diffHours = diff / (60 * 60 * 1000) % 24;
Log.e("test",diffHours + " hours, ");
}
catch (Exception e)
{
// TODO: handle exception
}
先生,您可以轻松使用java功能. long difference = date2.getTime() – date1.getTime();看看这 link 这将对你有所帮助.
翻译自:https://stackoverflow.com/questions/18908738/calculate-difference-between-two-times-android