-
Notifications
You must be signed in to change notification settings - Fork 128
[Snippets] java date/time snippets #188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
301e8fe
java date-time formatting snippets
Mcbencrafter 3cb58a5
added duration formatting
Mcbencrafter 6b36555
removed language from tags
Mcbencrafter fd4bac7
enforced formatting requirements
Mcbencrafter d64cbef
Merge branch 'dostonnabotov:main' into main
Mcbencrafter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
snippets/java/date-time/date-time-formatting-american.md
This file contains hidden or 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" | ||
``` |
32 changes: 32 additions & 0 deletions
snippets/java/date-time/date-time-formatting-european.md
This file contains hidden or 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
snippets/java/date-time/duration-formatting-hours-minutes-seconds.md
This file contains hidden or 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
snippets/java/date-time/duration-formatting-minutes-seconds.md
This file contains hidden or 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" | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.