计算两次android之间的差异 [英] Calculate difference between two times android
本文介绍了计算两次android之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发和android应用程序,我需要计算两次之间的时间差.我需要计算24小时的时间差,以及两天的时间差(例如今天下午5点到明天上午9点).
I am developing and android app, where I need to calculate the difference between two times.I need to calculate the time difference for 24 hrs, and also the difference between times on two days(Eg. 5pm today to 9 am tomorrow).
我已经尝试了以下代码,以计算仅适用于 24 小时的差异,
I have tried the below code, to calculate the difference which works only for 24 hrs,
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 这将对您有所帮助.
Sir, you can make it easily in using java feature. long difference = date2.getTime() - date1.getTime();
Take a look in this link this will help you.
这篇关于计算两次android之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文