来自 android studio 列表的持续时间总和

Jam*_*rry 1 java time simpledateformat android-studio

我正在尝试计算考勤应用程序的持续时间列表的总和。我能够计算出 2 次的差异,但不确定如何遍历列表以将日期相加。

下面的代码是我到目前为止所尝试的。列表中有 2 个持续时间,它们使用对象类从 Firebase 添加到列表中。

持续时间 1 = 0:5:42
持续时间 2 = 0:0:09
预期总数 = 0:5:51

//初始化

long sum;
Run Code Online (Sandbox Code Playgroud)

//当前方法尝试

public void grandTotal() throws ParseException {
        java.util.Date date1;
        java.util.Date date2;

        DateFormat formatter = new SimpleDateFormat("hh:mm:ss");
        for(int i = 0 ; i < newList.size(); i++) {
            String str_time1 = newList.get(i).getDuration();
            date1 = formatter.parse(str_time1);

            for (int j = 1; j < newList.size(); j++) {
                String str_time2 = newList.get(j).getDuration();
                date2 = formatter.parse(str_time2);
                sum = date1.getTime() + date2.getTime();
            }
        }
        secs = sum/1000;

        mins = secs/60;
        secs%=60;
        hours = mins/60;
        mins%=60;
        txtDailyBalance.setText(hours+":"+mins+":"+String.format("%02d",secs));
    }
Run Code Online (Sandbox Code Playgroud)

我得到的结果输出是 = -1:59:42

我使用这种方法得到了解决方案,但我认为这不是一个好的做法,所以我一直在研究这篇文章中有关不同方法的更多信息,所以谢谢你。

//Sum of time durations for that day
public void grandTotal() throws ParseException {
    java.util.Date date1;
    java.util.Date date2;

    DateFormat formatter = new SimpleDateFormat("hh:mm:ss");
    for(int i = 0 ; i < newList.size(); i++) {
        timeDuration.add(newList.get(i).getDuration());
    }
    long tDur = 0;
    for (String tmp : timeDuration){
        String[] arr = tmp.split(":");
        tDur += Integer.parseInt(arr[2]);
        tDur += 60 * Integer.parseInt(arr[1]);
        tDur += 3600 * Integer.parseInt(arr[0]);
    }
    long hours = tDur / 3600;
    tDur %= 3600;
    long mins = tDur / 60;
    tDur %= 60;
    long secs = tDur;
    txtDailyBalance.setText(hours+":"+mins+":"+String.format("%02d",secs));
}
Run Code Online (Sandbox Code Playgroud)

Ole*_*.V. 5

正如我在评论中所说,在Duration持续时间内使用该类:

    List<Duration> durations = List.of(
            Duration.ofMinutes(5).plusSeconds(42), // 0:05:42
            Duration.ofSeconds(9),                 // 0:00:09
            Duration.parse("PT11M"));              // 0:11:00

    Duration sum = Duration.ZERO;
    for (Duration dur : durations) {
        sum = sum.plus(dur);
    }

    System.out.println(sum);
Run Code Online (Sandbox Code Playgroud)

这打印

PT16M51S
Run Code Online (Sandbox Code Playgroud)

所以16分51秒。

持续时间本身解析并生成 ISO 8601 格式,就像您刚刚看到的那样,PT16M51S. 如果您需要hh:mm:ss格式化持续时间字符串,我相信您可以编写用于格式化和解析该格式的辅助方法。对于格式化,Stack Overflow 上已经有几个问题,例如How to format a duration in java? (例如格式 H:MM:SS)(搜索更多)。

为什么不Date呢?

Date在一段时间内使用 a是不正确的。时间点和时间量是两个不同的概念。您看到的错误可能是由于时区偏移造成的,但在此过程中您可能会遇到其他错误,更糟糕的是,您的代码难以阅读且容易误解。

此外,Date该类已经过时并被java.time现代 Java 日期和时间 API 中的类所取代。相同的 API 引入了类Period(用于年、月和日)和Duration(用于小时、分钟、秒和秒的分数)。

问题:我可以在 Android 上使用 java.time 吗?

是的,您可以java.time在 Android 上使用。它只需要至少 Java 6

  • 在 Java 8 及更高版本以及更新的 Android 设备上,现代 API 是内置的。
  • 在 Java 6 和 7 中获得 ThreeTen Backport,新类的向后移植(ThreeTen for JSR 310;请参阅底部的链接)。
  • 在较旧的 Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保从org.threeten.bp子包中导入日期和时间类。

链接