计算两次之间的差异android [英] Calculate difference between two times android

查看:144
本文介绍了计算两次之间的差异android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发和Android应用程序,我需要计算两次之间的差异。我需要计算24小时的时差,以及两天的时间差异(例如今天下午5点到明天上午9点)



我尝试了以下代码,以计算仅在24小时工作的差异,

  String dateStart =08:00:00; 
String dateStop =13:00:00;

// HH以24小时格式转换小时(0-23),日计算
SimpleDateFormat format = new SimpleDateFormat(HH:mm:ss);

日期d1 = null;
日期d2 = null;

try
{
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);

//以毫秒为单位
long diff = d2.getTime() - d1.getTime();
long diffHours = diff /(60 * 60 * 1000)%24;
Log.e(test,diffHours +hours);
}
catch(异常e)
{
// TODO:handle exception
}


解决方案

主席先生,您可以轻松使用java功能。 long difference = date2.getTime() - date1.getTime(); 看看这个链接将帮助您。


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).

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
} 

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆