I have an array of duration stored as a string.
[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]
I want to calculate the duration so that it looks like: 21:59:56.
I am using time4j and my code is as follows:
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));
}
}
When I print duration i get: PT15H230M4565S
When I format the duration i get the following:
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)
Please help me I am not sure what I am missing out.
Your error message says: "Too many digits for: MINUTES [PT15H230M4565S]" (=230) but your pattern only defines two digits for the minute part. So my recommendation is to use the pattern char "#" for an optional digit. For example, if you know that there might be between 1 and four digits for the minute part then:
#h:###m:###s
One important hint though:
If you want your duration being normalized as much as possible (only the hour part potentially with more than 2 digits then use the pattern "###h:mm:ss" and apply a normalization before formatting/printing:
dur = dur.with(Duration.STD_CLOCK_PERIOD);
System.out.print(timeFormat.format(dur));
The final solution (here in a sketchy way) would use two formatters, one as parser and one as printer to take into account different patterns:
// 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
Your concrete result PT15H230M4565S
would then be printed as: 20:06:05