如何获取 Java 中数组中所有持续时间的总和?

How to get sum of all time duration in an array in Java?

我有一个存储为字符串的持续时间数组。

[0:0, 0:0, 0:0, 0:0, 0:0, 0:0, 0:0, 0:0, 0:0, 0:0, 0:0, 0:0, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:39, 0:00:33, 0:00:37, 0:04:22, 0:00:20, 0:00:55, 0:00:27, 0:00:20, 0:01:03, 0:00:35, 0:00:44, 0:00:33, 0:00:49, 0:00:24, 0:16:45, 0:00:31, 0:01:41, 0:01:39, 0:04:19, 0:00:17, 0:00:19, 0:01:39, 0:00:39, 0:01:29, 0:04:41, 0:03:17, 0:00:35, 0:01:25, 0:00:25, 0:01:23, 0:00:01, 0:00:24, 0:00:17, 0:00:17, 0:00:17, 0:00:17, 0:00:03, 0:00:02, 0:00:38, 0:00:38, 0:00:14, 0:00:04, 0:00:07, 0:00:13, 0:00:00, 0:00:11, 0:00:44, 0:00:03, 0:00:14, 0:00:05, 0:16:45, 0:00:10, 0:27:06, 0:00:08, 0:00:22, 0:03:10, 0:03:10, 0:00:25, 0:00:59, 0:00:14, 0:00:10, 0:00:23, 0:00:09, 0:00:10, 0:00:05, 0:00:37, 0:00:09, 0:00:16, 0:00:13, 0:00:16, 0:00:21, 0:14:03, 0:14:03, 0:03:29, 0:00:05, 0:00:05, 0:00:45, 0:00:04, 0:00:04, 0:01:43, 0:01:43, 0:00:53, 0:00:04, 0:00:04, 0:00:04, 0:00:04, 0:00:04, 0:00:04, 0:00:04, 0:00:04, 0:00:04, 0:00:04, 0:00:04, 0:00:04, 0:00:04, 0:00:04, 0:00:04, 0:00:04, 0:00:04, 0:00:04, 0:00:19, 0:00:19, 0:00:25, 0:00:11, 0:00:15, 0:00:15, 0:00:21, 0:00:22, 0:00:30, 0:00:06, 0:00:21, 0:00:56, 0:00:29, 0:00:19, 0:00:12, 0:00:29, 0:00:07, 0:00:01, 0:00:01, 0:00:03, 0:00:01, 0:00:04, 0:00:04, 0:00:03, 0:00:11, 0:00:09, 0:00:08, 0:00:31, 0:00:37, 0:00:08, 0:01:59, 0:00:23, 0:00:05, 0:00:27, 0:00:02, 0:00:03, 0:00:03, 0:00:05, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:09, 0:00:05, 0:00:28, 0:00:03, 0:00:13, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:00:00, 0:01:55, 0:00:21, 0:00:39, 0:00:00, 0:00:01, 0:00:18, 0:00:05, 0:00:07, 0:00:05, 0:00:44, 0:00:14, 0:01:54, 0:00:08, 0:00:03, 0:00:07, 0:00:07, 0:00:08, 0:00:31, 0:00:31, 0:00:05, 0:00:05, 0:00:07, 0:00:04, 0:00:09, 0:00:07, 0:00:45, 0:00:45, 0:00:07, 0:00:06, 0:00:09, 0:00:26, 0:00:09, 0:00:14, 0:00:12, 0:00:09, 0:00:00, 0:00:21, 0:00:05, 0:00:07, 0:00:07, 0:00:13, 0:00:13, 0:00:03, 0:00:33, 0:00:47, 0:00:11, 0:00:15, 0:00:01, 0:00:29, 0:00:08, 0:00:32, 0:00:05, 0:00:15, 0:00:10, 0:00:24, 0:00:35, 0:00:15, 0:00:07, 0:00:28, 0:00:13, 0:00:13, 0:00:05, 0:00:21, 0:00:15, 0:00:17, 0:00:11, 0:00:00, 0:01:55, 0:00:37, 0:00:04, 0:01:03, 0:00:30, 0:00:09, 0:02:04, 0:00:02, 0:00:03, 0:00:29, 0:00:29, 0:00:29, 0:00:12, 0:00:01, 0:00:00, 0:00:25, 0:00:25, 0:00:13, 0:00:29, 0:00:13, 0:01:45, 0:01:16, 0:00:13, 0:20:41, 0:01:55, 0:00:24, 0:00:05, 0:00:31, 0:00:18, 0:00:24, 0:00:10, 0:03:27, 00:16:00, 00:07:00, 00:09:00, 00:02:00, 00:09:00, 02:00:29, 02:00:19, 02:00:17, 02:00:04, 02:01:47, 3:20:37, 02:00:04, 00:00:02, 00:00:12, 00:02:06, 00:00:02, 00:00:58, 00:10:47, 00:01:01]

我想计算持续时间,使其看起来像:21:59:56。

我使用的是time4j,我的代码如下:

 Duration.Formatter<ClockUnit> timeFormat =
                    Duration.Formatter.ofPattern(ClockUnit.class, "hh:mm:ss");
            Duration<ClockUnit> dur = Duration.ofZero();
            long sum =0;
            for (DataSnapshot w : dataSnapshot.getChildren()) {
                EmployeeDTO d = w.getValue(EmployeeDTO.class);
                if (d.getEmpHours() != null && !d.getEmpHours().equals("")) {

                    payme.add(d.getEmpHours());
                    try {
                        dur = dur.plus(timeFormat.parse(d.getEmpHours()));
                    } catch (ParseException e) {

                    }


      System.out.print(timeFormat.format(dur));
    }
}

当我打印持续时间时,我得到:PT15H230M4565S

当我格式化持续时间时,我得到以下信息:

04-05 12:00:16.347 9619-9619/? E/UncaughtException: java.lang.IllegalArgumentException: Too many digits for: MINUTES [PT15H230M4565S]
                                                    at net.time4j.format.TimeSpanFormatter$NumberItem.print(Unknown Source)
                                                    at net.time4j.format.TimeSpanFormatter.print(Unknown Source)
                                                    at net.time4j.format.TimeSpanFormatter.format(Unknown Source)
                                                    at util.NetworkUtil.onDataChange(NetworkUtil.java:266)
                                                    at com.google.android.gms.internal.zzegf.zza(Unknown Source)
                                                    at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source)
                                                    at com.google.android.gms.internal.zzeig.run(Unknown Source)
                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                    at android.os.Looper.loop(Looper.java:145)
                                                    at android.app.ActivityThread.main(ActivityThread.java:6934)
                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

请帮助我,我不确定我错过了什么。

您的错误消息显示:"Too many digits for: MINUTES [PT15H230M4565S]" (=230) 但您的模式只为分钟部分定义了两位数。所以我的建议是对 optional digit 使用模式字符“#”。例如,如果您知道分钟部分可能有 1 到 4 位数字,则:

#h:###m:###s

一个重要的提示:

如果您希望您的持续时间尽可能标准化(只有小时部分可能超过 2 位数字,那么使用模式“###h:mm:ss” 并在 formatting/printing:

之前应用归一化
dur = dur.with(Duration.STD_CLOCK_PERIOD);
System.out.print(timeFormat.format(dur));

最终解决方案(这里是粗略的)将使用两个格式化程序,一个作为解析器,一个作为打印机帐户不同模式:

// initialization of parser and duration
Duration.Formatter<ClockUnit> parser =
    Duration.Formatter.ofPattern(ClockUnit.class, "#h:#m[:#s]");
Duration<ClockUnit> d = Duration.ofZero();

// parse the durations inside your loop and sum up
d = d.plus(parser.parse("0:0")); // PT0S
d = d.plus(parser.parse("0:00:00")); // PT0S

// here the final evaluation of the result
d = d.with(Duration.STD_CLOCK_PERIOD);
Duration.Formatter<ClockUnit> printer =
        Duration.Formatter.ofPattern(ClockUnit.class, "##hh:mm:ss");
System.out.println(printer.format(d)); // 00:00:00

您的具体结果 PT15H230M4565S 将打印为:20:06:05