java – 计算两次android之间的差异

栏目: Android · 发布时间: 6年前

内容简介:先生,您可以轻松使用java功能. long difference = date2.getTime() – date1.getTime();看看这翻译自:https://stackoverflow.com/questions/18908738/calculate-difference-between-two-times-android
我正在开发和 Android

应用程序,我需要计算两次之间的差异.我需要计算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


以上所述就是小编给大家介绍的《java – 计算两次android之间的差异》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Just My Type

Just My Type

Simon Garfield / Profile Books / 2010-10-21 / GBP 14.99

What's your type? Suddenly everyone's obsessed with fonts. Whether you're enraged by Ikea's Verdanagate, want to know what the Beach Boys have in common with easy Jet or why it's okay to like Comic Sa......一起来看看 《Just My Type》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具