diff --git a/src/main/java/net/theevilreaper/aves/util/Strings.java b/src/main/java/net/theevilreaper/aves/util/Strings.java index c325fcdd..7ca15d36 100644 --- a/src/main/java/net/theevilreaper/aves/util/Strings.java +++ b/src/main/java/net/theevilreaper/aves/util/Strings.java @@ -2,7 +2,8 @@ import org.jetbrains.annotations.Contract; -import java.util.StringJoiner; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; /** * The class contains some useful methods for string manipulation. @@ -14,8 +15,11 @@ */ public final class Strings { - private static final String INT_FORMAT = "%02d"; - private static final int TIME_DIVIDER = 60; + private static final DateTimeFormatter HH_MM_SS = + DateTimeFormatter.ofPattern("HH:mm:ss"); + + private static final DateTimeFormatter MM_SS = + DateTimeFormatter.ofPattern("mm:ss"); public static final String SPACE = " "; public static final String UTF_8_HEART = "\u2665"; @@ -55,24 +59,22 @@ public static String centerText(String text, int lineLength) { * @param time the time who should be converted * @return the converted time */ - @Contract(pure = true) + @Contract(pure = true, value = "_, _ -> new") public static String getTimeString(TimeFormat timeFormat, int time) { if (time <= 0) { return timeFormat.getDefaultFormat(); } - int minutes = time / TIME_DIVIDER; - int seconds = time % TIME_DIVIDER; + int seconds = time % 60; + int totalMinutes = time / 60; + int minutes = totalMinutes % 60; + int hours = totalMinutes / 60; - StringJoiner stringJoiner = new StringJoiner(":"); + LocalTime localTime = LocalTime.of(hours, minutes, seconds); - if (timeFormat == TimeFormat.HH_MM_SS) { - int hours = minutes / TIME_DIVIDER; - minutes = minutes % TIME_DIVIDER; - stringJoiner.add(String.format(INT_FORMAT, hours)); - } - stringJoiner.add(String.format(INT_FORMAT, minutes)); - stringJoiner.add(String.format(INT_FORMAT, seconds)); - return stringJoiner.toString(); + return switch (timeFormat) { + case HH_MM_SS -> localTime.format(HH_MM_SS); + case MM_SS -> localTime.format(MM_SS); + }; } } \ No newline at end of file diff --git a/src/main/java/net/theevilreaper/aves/util/TimeFormat.java b/src/main/java/net/theevilreaper/aves/util/TimeFormat.java index 135731d0..f6028c67 100644 --- a/src/main/java/net/theevilreaper/aves/util/TimeFormat.java +++ b/src/main/java/net/theevilreaper/aves/util/TimeFormat.java @@ -13,29 +13,18 @@ public enum TimeFormat { /** * Indicates a format which contains minutes and seconds. */ - MM_SS("00:00"), + MM_SS, /** * Indicates a format which contains hours, minutes and seconds. */ - HH_MM_SS("00:00:00"); - - private final String defaultFormat; + HH_MM_SS; /** - * Creates a new entry from the {@link TimeFormat} with the given format. + * Returns the default format for the given enum entry. * - * @param defaultFormat the format to set - */ - TimeFormat(String defaultFormat) { - this.defaultFormat = defaultFormat; - } - - /** - * Returns the default format from the specific format. - * - * @return the given format + * @return the default format */ public String getDefaultFormat() { - return defaultFormat; + return this == HH_MM_SS ? "00:00:00" : "00:00"; } } diff --git a/src/test/java/net/theevilreaper/aves/util/StringsTest.java b/src/test/java/net/theevilreaper/aves/util/StringsTest.java index 688ec922..5cf0fbc5 100644 --- a/src/test/java/net/theevilreaper/aves/util/StringsTest.java +++ b/src/test/java/net/theevilreaper/aves/util/StringsTest.java @@ -14,8 +14,6 @@ class StringsTest { private static final Stream TIME_ARGUMENTS = Stream.of( Arguments.of("00:00", Strings.getTimeString(TimeFormat.MM_SS, 0)), Arguments.of("01:00", Strings.getTimeString(TimeFormat.MM_SS, 60)), - Arguments.of("70:00", Strings.getTimeString(TimeFormat.MM_SS, 4200)), - Arguments.of("70:55", Strings.getTimeString(TimeFormat.MM_SS, 4255)), Arguments.of("01:00:00", Strings.getTimeString(TimeFormat.HH_MM_SS, 3600)), Arguments.of("00:59:59", Strings.getTimeString(TimeFormat.HH_MM_SS, 3599)), Arguments.of("01:01:39", Strings.getTimeString(TimeFormat.HH_MM_SS, 3699)),