From 301e8fe7ff65bce1c09554930469c3bb11c2d1b2 Mon Sep 17 00:00:00 2001 From: Mcbencrafter Date: Sun, 5 Jan 2025 10:22:31 +0100 Subject: [PATCH 1/4] java date-time formatting snippets --- .../date-time-formatting-american.md | 36 +++++++++++++++++++ .../date-time-formatting-european.md | 36 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 snippets/java/date-time/date-time-formatting-american.md create mode 100644 snippets/java/date-time/date-time-formatting-european.md diff --git a/snippets/java/date-time/date-time-formatting-american.md b/snippets/java/date-time/date-time-formatting-american.md new file mode 100644 index 00000000..c8a7cec7 --- /dev/null +++ b/snippets/java/date-time/date-time-formatting-american.md @@ -0,0 +1,36 @@ +--- +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: java, 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; + +public class DateTimeFormatterAmerican { + + public static void main(String[] args) { + 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" + } + + // 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) + )); + } +} +``` \ No newline at end of file diff --git a/snippets/java/date-time/date-time-formatting-european.md b/snippets/java/date-time/date-time-formatting-european.md new file mode 100644 index 00000000..01a75895 --- /dev/null +++ b/snippets/java/date-time/date-time-formatting-european.md @@ -0,0 +1,36 @@ +--- +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: java, 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; + +public class DateTimeFormatterEuropean { + + public static void main(String[] args) { + 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" + } + + // 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) + )); + } +} +``` \ No newline at end of file From 3cb58a50500aef2c28e66fb7a85d823c83bbd217 Mon Sep 17 00:00:00 2001 From: Mcbencrafter Date: Sun, 5 Jan 2025 10:23:07 +0100 Subject: [PATCH 2/4] added duration formatting --- ...ration-formatting-hours-minutes-seconds.md | 39 +++++++++++++++++++ .../duration-formatting-minutes-seconds.md | 32 +++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 snippets/java/date-time/duration-formatting-hours-minutes-seconds.md create mode 100644 snippets/java/date-time/duration-formatting-minutes-seconds.md diff --git a/snippets/java/date-time/duration-formatting-hours-minutes-seconds.md b/snippets/java/date-time/duration-formatting-hours-minutes-seconds.md new file mode 100644 index 00000000..11110391 --- /dev/null +++ b/snippets/java/date-time/duration-formatting-hours-minutes-seconds.md @@ -0,0 +1,39 @@ +--- +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: java, time, formatting, hours, minutes, seconds +--- + +```java +package snippets.time; + +import java.util.concurrent.TimeUnit; + +public class DurationFormatterHoursMinutesSeconds { + + public static void main(String[] args) { + System.out.println(formatDurationToHoursMinutesAndSeconds(3810, TimeUnit.SECONDS, true)); // "01:03:30" + System.out.println(formatDurationToHoursMinutesAndSeconds(3810, TimeUnit.SECONDS, false)); // "01:03" + } + + 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); + } + } +} +``` \ No newline at end of file diff --git a/snippets/java/date-time/duration-formatting-minutes-seconds.md b/snippets/java/date-time/duration-formatting-minutes-seconds.md new file mode 100644 index 00000000..979a7ec9 --- /dev/null +++ b/snippets/java/date-time/duration-formatting-minutes-seconds.md @@ -0,0 +1,32 @@ +--- +title: Duration formatting minutes seconds +description: Converts a given time duration to a human-readable string in the format "mm:ss" +author: Mcbencrafter +tags: java, time, formatting, minutes, seconds +--- + +```java +import java.util.concurrent.TimeUnit; + +class DurationFormatterMinutesSeconds { + + public static void main(String[] args) { + System.out.println(formatDurationToMinutesAndSeconds(120, TimeUnit.SECONDS)); // "02:00" + System.out.println(formatDurationToMinutesAndSeconds(75, TimeUnit.SECONDS)); // "01:15" + } + + 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); + } +} +``` \ No newline at end of file From 6b36555fa0aef1ef43bdf66c2ec0bde056a16aa9 Mon Sep 17 00:00:00 2001 From: Mcbencrafter Date: Sun, 5 Jan 2025 14:09:56 +0100 Subject: [PATCH 3/4] removed language from tags --- snippets/java/date-time/date-time-formatting-american.md | 2 +- snippets/java/date-time/date-time-formatting-european.md | 2 +- .../java/date-time/duration-formatting-hours-minutes-seconds.md | 2 +- snippets/java/date-time/duration-formatting-minutes-seconds.md | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/snippets/java/date-time/date-time-formatting-american.md b/snippets/java/date-time/date-time-formatting-american.md index c8a7cec7..0dac92f1 100644 --- a/snippets/java/date-time/date-time-formatting-american.md +++ b/snippets/java/date-time/date-time-formatting-american.md @@ -2,7 +2,7 @@ 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: java, date, time, date-time, formatting, american +tags: date, time, date-time, formatting, american --- ```java diff --git a/snippets/java/date-time/date-time-formatting-european.md b/snippets/java/date-time/date-time-formatting-european.md index 01a75895..fed0f407 100644 --- a/snippets/java/date-time/date-time-formatting-european.md +++ b/snippets/java/date-time/date-time-formatting-european.md @@ -2,7 +2,7 @@ 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: java, date, time, date-time, formatting, european +tags: date, time, date-time, formatting, european --- ```java diff --git a/snippets/java/date-time/duration-formatting-hours-minutes-seconds.md b/snippets/java/date-time/duration-formatting-hours-minutes-seconds.md index 11110391..872a0f22 100644 --- a/snippets/java/date-time/duration-formatting-hours-minutes-seconds.md +++ b/snippets/java/date-time/duration-formatting-hours-minutes-seconds.md @@ -2,7 +2,7 @@ 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: java, time, formatting, hours, minutes, seconds +tags: time, formatting, hours, minutes, seconds --- ```java diff --git a/snippets/java/date-time/duration-formatting-minutes-seconds.md b/snippets/java/date-time/duration-formatting-minutes-seconds.md index 979a7ec9..bdb82979 100644 --- a/snippets/java/date-time/duration-formatting-minutes-seconds.md +++ b/snippets/java/date-time/duration-formatting-minutes-seconds.md @@ -2,7 +2,7 @@ title: Duration formatting minutes seconds description: Converts a given time duration to a human-readable string in the format "mm:ss" author: Mcbencrafter -tags: java, time, formatting, minutes, seconds +tags: time, formatting, minutes, seconds --- ```java From fd4bac7ad241cd54b0283c6769db4ef7e133e528 Mon Sep 17 00:00:00 2001 From: Mcbencrafter Date: Mon, 6 Jan 2025 17:22:47 +0100 Subject: [PATCH 4/4] enforced formatting requirements --- .../date-time-formatting-american.md | 38 +++++++------- .../date-time-formatting-european.md | 38 +++++++------- ...ration-formatting-hours-minutes-seconds.md | 50 ++++++++----------- .../duration-formatting-minutes-seconds.md | 32 ++++++------ 4 files changed, 70 insertions(+), 88 deletions(-) diff --git a/snippets/java/date-time/date-time-formatting-american.md b/snippets/java/date-time/date-time-formatting-american.md index 0dac92f1..c2bf6f93 100644 --- a/snippets/java/date-time/date-time-formatting-american.md +++ b/snippets/java/date-time/date-time-formatting-american.md @@ -2,7 +2,7 @@ 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 +tags: date,time,date-time,formatting,american --- ```java @@ -11,26 +11,22 @@ import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.concurrent.TimeUnit; -public class DateTimeFormatterAmerican { - - public static void main(String[] args) { - 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" - } +// using the system default time zone +public static String formatDateTimeAmerican(long time, TimeUnit timeUnit) { + return formatDateTimeAmerican(time, timeUnit, ZoneId.systemDefault()); +} - // 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) - )); - } +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" ``` \ No newline at end of file diff --git a/snippets/java/date-time/date-time-formatting-european.md b/snippets/java/date-time/date-time-formatting-european.md index fed0f407..7c78eba2 100644 --- a/snippets/java/date-time/date-time-formatting-european.md +++ b/snippets/java/date-time/date-time-formatting-european.md @@ -2,7 +2,7 @@ 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 +tags: date,time,date-time,formatting,european --- ```java @@ -11,26 +11,22 @@ import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.concurrent.TimeUnit; -public class DateTimeFormatterEuropean { - - public static void main(String[] args) { - 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" - } +// using the system default time zone +public static String formatDateTimeEuropean(long time, TimeUnit timeUnit) { + return formatDateTimeEuropean(time, timeUnit, ZoneId.systemDefault()); +} - // 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) - )); - } +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" ``` \ No newline at end of file diff --git a/snippets/java/date-time/duration-formatting-hours-minutes-seconds.md b/snippets/java/date-time/duration-formatting-hours-minutes-seconds.md index 872a0f22..718fd39b 100644 --- a/snippets/java/date-time/duration-formatting-hours-minutes-seconds.md +++ b/snippets/java/date-time/duration-formatting-hours-minutes-seconds.md @@ -2,38 +2,32 @@ 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 +tags: time,formatting,hours,minutes,seconds --- ```java -package snippets.time; - import java.util.concurrent.TimeUnit; - -public class DurationFormatterHoursMinutesSeconds { - - public static void main(String[] args) { - System.out.println(formatDurationToHoursMinutesAndSeconds(3810, TimeUnit.SECONDS, true)); // "01:03:30" - System.out.println(formatDurationToHoursMinutesAndSeconds(3810, TimeUnit.SECONDS, false)); // "01:03" - } - - 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); - } + +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" ``` \ No newline at end of file diff --git a/snippets/java/date-time/duration-formatting-minutes-seconds.md b/snippets/java/date-time/duration-formatting-minutes-seconds.md index bdb82979..49107f58 100644 --- a/snippets/java/date-time/duration-formatting-minutes-seconds.md +++ b/snippets/java/date-time/duration-formatting-minutes-seconds.md @@ -2,31 +2,27 @@ 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 +tags: time,formatting,minutes,seconds --- ```java import java.util.concurrent.TimeUnit; -class DurationFormatterMinutesSeconds { +public static String formatDurationToMinutesAndSeconds(int time, TimeUnit timeUnit) { + long totalSeconds = timeUnit.toSeconds(time); - public static void main(String[] args) { - System.out.println(formatDurationToMinutesAndSeconds(120, TimeUnit.SECONDS)); // "02:00" - System.out.println(formatDurationToMinutesAndSeconds(75, TimeUnit.SECONDS)); // "01:15" - } + if (totalSeconds < 0) + throw new IllegalArgumentException("Duration must be a non-negative value."); - public static String formatDurationToMinutesAndSeconds(int time, TimeUnit timeUnit) { - long totalSeconds = timeUnit.toSeconds(time); + // 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; - 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); - } + 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" ``` \ No newline at end of file