【问题标题】:Calculate Time difference between 2 given timestamps only considering day time of the day in Android?仅考虑Android中一天中的一天时间计算2个给定时间戳之间的时间差?
【发布时间】:2020-01-30 12:23:37
【问题描述】:

我有 2 个不同的纪元时间戳 A 和 B。对于每一天,我都有不同的日出和日落时间。 假设第 1 天,即时间戳,一天的日出时间是早上 6.00,日落时间是下午 6.30,而对于第 2 天,日出时间是早上 6.05,日落时间是下午 6.25,等等,下面是我的格式,

val sunRise = mapOf<Int,String>(1 to "6.00", 2 to "6.05",3 to "6.01", 4 to "6.06")
val sunSet = mapOf<Int,String>(1 to "18.30", 2 to "18.25",3 to "18.20", 4 to "18.23")
val startTime = 1579919400000
val endTime = 1580203800000

现在如何计算这些时间戳之间的时间,时间戳仅在 Android 中的 sunRise 和 sunSet 之间

【问题讨论】:

    标签: android timestamp


    【解决方案1】:

    尝试以下方法将计算两次之间的差异。

    System.out.println("==result==: " + stringForTime(endTime - startTime));
    public String stringForTime(long timeMs) {
            long totalSeconds = timeMs / 1000;
    
            long seconds = totalSeconds % 60;
            long minutes = (totalSeconds / 60) % 60;
            long hours = totalSeconds / 3600;
    
            Formatter mFormatter = new Formatter();
    
            if (hours > 0) {
                return mFormatter.format("%02d:%02d:%02d", hours, minutes, seconds).toString();
            } else {
                return mFormatter.format("%02d:%02d", minutes, seconds).toString();
            }
        }
    

    【讨论】:

    • 这只是给出了 2 个时间戳之间的时间差,但想要的是每天给定 timePeriod 之间的时间。你能把我的问题看一遍吗?
    猜你喜欢
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多