Skip to content

Commit 9181837

Browse files
Merge pull request #188 from Mcbencrafter/main
[Snippets] java date/time snippets
2 parents 3dee709 + d64cbef commit 9181837

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: Date time formatting american
3+
description: Formats a timestamp to a human-readable date-time string in the format "MM/dd/yyyy hh:mm:ss a"
4+
author: Mcbencrafter
5+
tags: date,time,date-time,formatting,american
6+
---
7+
8+
```java
9+
import java.time.Instant;
10+
import java.time.ZoneId;
11+
import java.time.format.DateTimeFormatter;
12+
import java.util.concurrent.TimeUnit;
13+
14+
// using the system default time zone
15+
public static String formatDateTimeAmerican(long time, TimeUnit timeUnit) {
16+
return formatDateTimeAmerican(time, timeUnit, ZoneId.systemDefault());
17+
}
18+
19+
public static String formatDateTimeAmerican(long time, TimeUnit timeUnit, ZoneId timeZone) {
20+
return DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a")
21+
.withZone(
22+
timeZone != null ? timeZone : ZoneId.systemDefault()
23+
)
24+
.format(Instant.ofEpochSecond(
25+
timeUnit.toSeconds(time)
26+
));
27+
}
28+
29+
// Usage:
30+
System.out.println(formatDateTimeAmerican(1735689599, TimeUnit.SECONDS)); // "12/31/2024 | 11:59:59 PM" for GMT+0000
31+
System.out.println(formatDateTimeAmerican(1735689599, TimeUnit.SECONDS, ZoneId.of("GMT+0000"))); // "12/31/2024 | 11:59:59 PM"
32+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: Date time formatting european
3+
description: Formats a timestamp to a human-readable date-time string in the format "dd.MM.yyyy HH:mm:ss"
4+
author: Mcbencrafter
5+
tags: date,time,date-time,formatting,european
6+
---
7+
8+
```java
9+
import java.time.Instant;
10+
import java.time.ZoneId;
11+
import java.time.format.DateTimeFormatter;
12+
import java.util.concurrent.TimeUnit;
13+
14+
// using the system default time zone
15+
public static String formatDateTimeEuropean(long time, TimeUnit timeUnit) {
16+
return formatDateTimeEuropean(time, timeUnit, ZoneId.systemDefault());
17+
}
18+
19+
public static String formatDateTimeEuropean(long time, TimeUnit timeUnit, ZoneId timeZone) {
20+
return DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss")
21+
.withZone(
22+
timeZone != null ? timeZone : ZoneId.systemDefault()
23+
)
24+
.format(Instant.ofEpochSecond(
25+
timeUnit.toSeconds(time)
26+
));
27+
}
28+
29+
// Usage:
30+
System.out.println(formatDateTimeEuropean(1735689599, TimeUnit.SECONDS)); // "31.12.2024 | 23:59:59" for GMT+0000
31+
System.out.println(formatDateTimeEuropean(1735689599, TimeUnit.SECONDS, ZoneId.of("GMT+0000"))); // "31.12.2024 | 23:59:59"
32+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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: time,formatting,hours,minutes,seconds
6+
---
7+
8+
```java
9+
import java.util.concurrent.TimeUnit;
10+
11+
public static String formatDurationToHoursMinutesAndSeconds(int time, TimeUnit timeUnit, boolean showSeconds) {
12+
long totalSeconds = timeUnit.toSeconds(time);
13+
14+
if (totalSeconds < 0)
15+
throw new IllegalArgumentException("Duration must be a non-negative value.");
16+
17+
// These variables can be directly used in the return statement,
18+
// but are kept as separate variables here for better readability.
19+
long hours = totalSeconds / 3600;
20+
long minutes = (totalSeconds % 3600) / 60;
21+
long seconds = totalSeconds % 60;
22+
23+
if (showSeconds) {
24+
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
25+
} else {
26+
return String.format("%02d:%02d", hours, minutes);
27+
}
28+
}
29+
30+
// Usage:
31+
System.out.println(formatDurationToHoursMinutesAndSeconds(3810, TimeUnit.SECONDS, true)); // "01:03:30"
32+
System.out.println(formatDurationToHoursMinutesAndSeconds(3810, TimeUnit.SECONDS, false)); // "01:03"
33+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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: time,formatting,minutes,seconds
6+
---
7+
8+
```java
9+
import java.util.concurrent.TimeUnit;
10+
11+
public static String formatDurationToMinutesAndSeconds(int time, TimeUnit timeUnit) {
12+
long totalSeconds = timeUnit.toSeconds(time);
13+
14+
if (totalSeconds < 0)
15+
throw new IllegalArgumentException("Duration must be a non-negative value.");
16+
17+
// These variables can be directly used in the return statement,
18+
// but are kept here as separate variables for better readability.
19+
long minutes = totalSeconds / 60;
20+
long seconds = totalSeconds % 60;
21+
22+
return String.format("%02d:%02d", minutes, seconds);
23+
}
24+
25+
// Usage:
26+
System.out.println(formatDurationToMinutesAndSeconds(120, TimeUnit.SECONDS)); // "02:00"
27+
System.out.println(formatDurationToMinutesAndSeconds(75, TimeUnit.SECONDS)); // "01:15"
28+
```

0 commit comments

Comments
 (0)