Skip to content

Commit

Permalink
Merge pull request #188 from Mcbencrafter/main
Browse files Browse the repository at this point in the history
[Snippets] java date/time snippets
  • Loading branch information
Mathys-Gasnier authored Jan 7, 2025
2 parents 3dee709 + d64cbef commit 9181837
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
32 changes: 32 additions & 0 deletions snippets/java/date-time/date-time-formatting-american.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: Date time formatting american
description: Formats a timestamp to a human-readable date-time string in the format "MM/dd/yyyy hh:mm:ss a"
author: Mcbencrafter
tags: date,time,date-time,formatting,american
---

```java
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

// using the system default time zone
public static String formatDateTimeAmerican(long time, TimeUnit timeUnit) {
return formatDateTimeAmerican(time, timeUnit, ZoneId.systemDefault());
}

public static String formatDateTimeAmerican(long time, TimeUnit timeUnit, ZoneId timeZone) {
return DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a")
.withZone(
timeZone != null ? timeZone : ZoneId.systemDefault()
)
.format(Instant.ofEpochSecond(
timeUnit.toSeconds(time)
));
}

// Usage:
System.out.println(formatDateTimeAmerican(1735689599, TimeUnit.SECONDS)); // "12/31/2024 | 11:59:59 PM" for GMT+0000
System.out.println(formatDateTimeAmerican(1735689599, TimeUnit.SECONDS, ZoneId.of("GMT+0000"))); // "12/31/2024 | 11:59:59 PM"
```
32 changes: 32 additions & 0 deletions snippets/java/date-time/date-time-formatting-european.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: Date time formatting european
description: Formats a timestamp to a human-readable date-time string in the format "dd.MM.yyyy HH:mm:ss"
author: Mcbencrafter
tags: date,time,date-time,formatting,european
---

```java
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

// using the system default time zone
public static String formatDateTimeEuropean(long time, TimeUnit timeUnit) {
return formatDateTimeEuropean(time, timeUnit, ZoneId.systemDefault());
}

public static String formatDateTimeEuropean(long time, TimeUnit timeUnit, ZoneId timeZone) {
return DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss")
.withZone(
timeZone != null ? timeZone : ZoneId.systemDefault()
)
.format(Instant.ofEpochSecond(
timeUnit.toSeconds(time)
));
}

// Usage:
System.out.println(formatDateTimeEuropean(1735689599, TimeUnit.SECONDS)); // "31.12.2024 | 23:59:59" for GMT+0000
System.out.println(formatDateTimeEuropean(1735689599, TimeUnit.SECONDS, ZoneId.of("GMT+0000"))); // "31.12.2024 | 23:59:59"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: Duration formatting hours minutes seconds
description: Converts a given time duration to a human-readable string in the format "hh:mm(:ss)"
author: Mcbencrafter
tags: time,formatting,hours,minutes,seconds
---

```java
import java.util.concurrent.TimeUnit;

public static String formatDurationToHoursMinutesAndSeconds(int time, TimeUnit timeUnit, boolean showSeconds) {
long totalSeconds = timeUnit.toSeconds(time);

if (totalSeconds < 0)
throw new IllegalArgumentException("Duration must be a non-negative value.");

// These variables can be directly used in the return statement,
// but are kept as separate variables here for better readability.
long hours = totalSeconds / 3600;
long minutes = (totalSeconds % 3600) / 60;
long seconds = totalSeconds % 60;

if (showSeconds) {
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
} else {
return String.format("%02d:%02d", hours, minutes);
}
}

// Usage:
System.out.println(formatDurationToHoursMinutesAndSeconds(3810, TimeUnit.SECONDS, true)); // "01:03:30"
System.out.println(formatDurationToHoursMinutesAndSeconds(3810, TimeUnit.SECONDS, false)); // "01:03"
```
28 changes: 28 additions & 0 deletions snippets/java/date-time/duration-formatting-minutes-seconds.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: Duration formatting minutes seconds
description: Converts a given time duration to a human-readable string in the format "mm:ss"
author: Mcbencrafter
tags: time,formatting,minutes,seconds
---

```java
import java.util.concurrent.TimeUnit;

public static String formatDurationToMinutesAndSeconds(int time, TimeUnit timeUnit) {
long totalSeconds = timeUnit.toSeconds(time);

if (totalSeconds < 0)
throw new IllegalArgumentException("Duration must be a non-negative value.");

// These variables can be directly used in the return statement,
// but are kept here as separate variables for better readability.
long minutes = totalSeconds / 60;
long seconds = totalSeconds % 60;

return String.format("%02d:%02d", minutes, seconds);
}

// Usage:
System.out.println(formatDurationToMinutesAndSeconds(120, TimeUnit.SECONDS)); // "02:00"
System.out.println(formatDurationToMinutesAndSeconds(75, TimeUnit.SECONDS)); // "01:15"
```

0 comments on commit 9181837

Please sign in to comment.