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

如何在Java中获得数组中所有持续时间的总和?,java,duration,time4j,Java,Duration,Time4j,我有一个以字符串形式存储的持续时间数组 [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: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]的数字太多”(=230),但您的模式仅为分钟部分定义了两个数字。因此,我的建议是使用模式char“#”表示。例如,如果您知道分钟部分可能有1到4位数字,则:

#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
//解析器的初始化和持续时间
格式分析器=
模式的Duration.Formatter.of(ClockUnit.class,“#h:#m[:#s]”);
持续时间d=零的持续时间();
//分析循环中的持续时间并总结
d=d.plus(parser.parse(“0:0”);//PT0S
d=d.plus(parser.parse(“0:00:00”);//PT0S
//这里是对结果的最终评估
d=d,带(持续时间、标准时钟周期);
格式打印机=
模式的Duration.Formatter.of(ClockUnit.class,“##hh:mm:ss”);
System.out.println(printer.format(d));//00:00:00

你的具体结果将被打印为:
20:06:05

你能用H:mm:ss试试吗。你的小时数有时是0,有时是00我试过我得到了“不受支持的模式符号:H”@VeselinDavidov模式语法区分大小写(在Java中,通常情况下,描述有效字母,这里是特定于库的).啊,我不确定。我想这就像日期格式,H是0:24,H是0:12
// 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