Convert seconds to compound duration: Difference between revisions

Line 2,144:
 
=={{header|Java}}==
This is a relatively simple task in Java, using the modulus-remainder operator.
<syntaxhighlight lang="java">
String duration(int seconds) {
StringBuilder string = new StringBuilder();
if (seconds >= 604_800 /* 1 wk */) {
string.append("%,d wk".formatted(seconds / 604_800));
seconds %= 604_800;
}
if (seconds >= 86_400 /* 1 d */) {
if (!string.isEmpty()) string.append(", ");
string.append("%d d".formatted(seconds / 86_400));
seconds %= 86_400;
}
if (seconds >= 3600 /* 1 hr */) {
if (!string.isEmpty()) string.append(", ");
string.append("%d hr".formatted(seconds / 3600));
seconds %= 3600;
}
if (seconds >= 60 /* 1 min */) {
if (!string.isEmpty()) string.append(", ");
string.append("%d min".formatted(seconds / 60));
seconds %= 60;
}
if (seconds > 0) {
if (!string.isEmpty()) string.append(", ");
string.append("%d sec".formatted(seconds));
}
return string.toString();
}
</syntaxhighlight>
<pre>
2 hr, 59 sec
1 d
9 wk, 6 d, 10 hr, 40 min
</pre>
<br />
An alternate demonstration
<syntaxhighlight lang="java">public class CompoundDuration {
 
118

edits