Skip to content

Commit 3cb58a5

Browse files
committed
added duration formatting
1 parent 301e8fe commit 3cb58a5

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
title: Duration formatting hours minutes seconds
3+
description: Converts a given time duration to a human-readable string in the format "hh:mm(:ss)"
4+
author: Mcbencrafter
5+
tags: java, time, formatting, hours, minutes, seconds
6+
---
7+
8+
```java
9+
package snippets.time;
10+
11+
import java.util.concurrent.TimeUnit;
12+
13+
public class DurationFormatterHoursMinutesSeconds {
14+
15+
public static void main(String[] args) {
16+
System.out.println(formatDurationToHoursMinutesAndSeconds(3810, TimeUnit.SECONDS, true)); // "01:03:30"
17+
System.out.println(formatDurationToHoursMinutesAndSeconds(3810, TimeUnit.SECONDS, false)); // "01:03"
18+
}
19+
20+
public static String formatDurationToHoursMinutesAndSeconds(int time, TimeUnit timeUnit, boolean showSeconds) {
21+
long totalSeconds = timeUnit.toSeconds(time);
22+
23+
if (totalSeconds < 0)
24+
throw new IllegalArgumentException("Duration must be a non-negative value.");
25+
26+
// These variables can be directly used in the return statement,
27+
// but are kept as separate variables here for better readability.
28+
long hours = totalSeconds / 3600;
29+
long minutes = (totalSeconds % 3600) / 60;
30+
long seconds = totalSeconds % 60;
31+
32+
if (showSeconds) {
33+
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
34+
} else {
35+
return String.format("%02d:%02d", hours, minutes);
36+
}
37+
}
38+
}
39+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: Duration formatting minutes seconds
3+
description: Converts a given time duration to a human-readable string in the format "mm:ss"
4+
author: Mcbencrafter
5+
tags: java, time, formatting, minutes, seconds
6+
---
7+
8+
```java
9+
import java.util.concurrent.TimeUnit;
10+
11+
class DurationFormatterMinutesSeconds {
12+
13+
public static void main(String[] args) {
14+
System.out.println(formatDurationToMinutesAndSeconds(120, TimeUnit.SECONDS)); // "02:00"
15+
System.out.println(formatDurationToMinutesAndSeconds(75, TimeUnit.SECONDS)); // "01:15"
16+
}
17+
18+
public static String formatDurationToMinutesAndSeconds(int time, TimeUnit timeUnit) {
19+
long totalSeconds = timeUnit.toSeconds(time);
20+
21+
if (totalSeconds < 0)
22+
throw new IllegalArgumentException("Duration must be a non-negative value.");
23+
24+
// These variables can be directly used in the return statement,
25+
// but are kept here as separate variables for better readability.
26+
long minutes = totalSeconds / 60;
27+
long seconds = totalSeconds % 60;
28+
29+
return String.format("%02d:%02d", minutes, seconds);
30+
}
31+
}
32+
```

0 commit comments

Comments
 (0)