【问题标题】:Android, calculate the how long time between now and 'n'Android,计算从现在到'n'之间的时间
【发布时间】:2014-09-03 06:55:57
【问题描述】:

我有使用 AlarmManager 执行某些任务的方法,但我必须在执行它之前计算时间, 例如,当前时间是 10:24,任务必须在 11:00 执行,所以,我如何计算 10:24 到 11:00 之间的时间长度

示例 当前时间 = 10:30 目标时间 = 11:00

10:30 到 11:00 之间的长时间是 30 分钟,我会将其转换为毫秒

【问题讨论】:

    标签: android datetime


    【解决方案1】:
    You can get difference between two times using Date class in java
    
    Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
    Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
    
    getTimeDiff(date1,date2,TimeUnit.MINUTES);
    public static long getTimeDiff(Date date1, Date date2, TimeUnit timeUnit) {
        long diffInMillies = date2.getTime() - date1.getTime();
        return timeUnit.convert(diffInMillies,TimeUnit.MILLISECONDS);
    }
    

    【讨论】:

    • 谢谢,这很有帮助,无论如何,我可以使用日历吗?
    • 欢迎。是的,您也可以使用 Calendar 类来做到这一点
    猜你喜欢
    • 2015-11-18
    • 2021-09-20
    • 1970-01-01
    • 2015-06-29
    • 2018-05-22
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 2015-03-28
    相关资源
    最近更新 更多