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

标签 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: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)

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

最佳答案

您的错误消息显示:“MINUTES [PT15H230M4565S] 的数字太多”(=230) 但您的模式只为分钟部分定义了两位数字。所以我的建议是对 optional digit 使用模式字符“#”。例如,如果您知道分钟部分可能有 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

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

关于java - 如何在 Java 中获取数组中所有持续时间的总和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49669624/

相关文章:

java - 为什么这个 Java 泛型代码不能正常工作?

java - 有没有办法让 Arraylist 元素等于类构造函数?

laravel - 无法在 FFmpeg laravel 中获取持续时间

java - 从现在开始持续时间是多少天?

java - 使用 time4j 获取该月的天数

java - Android 启动时启动应用程序

java - 无法使用 appium 在模拟器中启动 Android 应用程序

java - 如何使用Java 8 Duration.of(amount,unit)方法?

php - 如何在不下载整个文件的情况下计算远程位置的 MP3 文件的持续时间?

java - 在 Time4j(波斯历)中定义日期范围