-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from Mcbencrafter/main
[Snippets] java date/time snippets
- Loading branch information
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
33 changes: 33 additions & 0 deletions
33
snippets/java/date-time/duration-formatting-hours-minutes-seconds.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
snippets/java/date-time/duration-formatting-minutes-seconds.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |