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

4

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

[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$7.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)

请帮助我,我不确定我漏掉了什么。


你能试试用 H:mm:ss 吗?你的小时有时是 0,有时是 00。 - Veselin Davidov
我已经尝试过了,但是出现了“不支持的模式符号:H”。 - Lawrence Hans Matjeni
@VeselinDavidov 模式语法区分大小写(与Java中一样,javadoc描述了有效字母,这里是特定于库的)。 - Meno Hochschild
啊,我不太确定。我以为它像日期格式一样,H代表0到24小时制,h代表0到12小时制。 - Veselin Davidov
1个回答

4
您的错误信息显示:“MINUTES [PT15H230M4565S]”(= 230)有太多位数,但是您的模式仅定义了分钟部分的两个数字。因此,我的建议是使用模式字符“#”来表示可选数字。例如,如果您知道分钟部分可能有1到四位数字,则:
#h:###m:###s

然而,这里有一个重要的提示:

如果您希望您的持续时间尽可能地被标准化(仅小时部分可能具有超过2个数字),那么请使用模式“###h:mm:ss”并在格式化/打印之前进行标准化:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接