22
33import org .jetbrains .annotations .Contract ;
44
5- import java .util .StringJoiner ;
5+ import java .time .LocalTime ;
6+ import java .time .format .DateTimeFormatter ;
67
78/**
89 * The class contains some useful methods for string manipulation.
1415 */
1516public final class Strings {
1617
17- private static final String INT_FORMAT = "%02d" ;
18- private static final int TIME_DIVIDER = 60 ;
18+ private static final DateTimeFormatter HH_MM_SS =
19+ DateTimeFormatter .ofPattern ("HH:mm:ss" );
20+
21+ private static final DateTimeFormatter MM_SS =
22+ DateTimeFormatter .ofPattern ("mm:ss" );
1923
2024 public static final String SPACE = " " ;
2125 public static final String UTF_8_HEART = "\u2665 " ;
@@ -55,24 +59,22 @@ public static String centerText(String text, int lineLength) {
5559 * @param time the time who should be converted
5660 * @return the converted time
5761 */
58- @ Contract (pure = true )
62+ @ Contract (pure = true , value = "_, _ -> new" )
5963 public static String getTimeString (TimeFormat timeFormat , int time ) {
6064 if (time <= 0 ) {
6165 return timeFormat .getDefaultFormat ();
6266 }
6367
64- int minutes = time / TIME_DIVIDER ;
65- int seconds = time % TIME_DIVIDER ;
68+ int seconds = time % 60 ;
69+ int totalMinutes = time / 60 ;
70+ int minutes = totalMinutes % 60 ;
71+ int hours = totalMinutes / 60 ;
6672
67- StringJoiner stringJoiner = new StringJoiner ( ":" );
73+ LocalTime localTime = LocalTime . of ( hours , minutes , seconds );
6874
69- if (timeFormat == TimeFormat .HH_MM_SS ) {
70- int hours = minutes / TIME_DIVIDER ;
71- minutes = minutes % TIME_DIVIDER ;
72- stringJoiner .add (String .format (INT_FORMAT , hours ));
73- }
74- stringJoiner .add (String .format (INT_FORMAT , minutes ));
75- stringJoiner .add (String .format (INT_FORMAT , seconds ));
76- return stringJoiner .toString ();
75+ return switch (timeFormat ) {
76+ case HH_MM_SS -> localTime .format (HH_MM_SS );
77+ case MM_SS -> localTime .format (MM_SS );
78+ };
7779 }
7880}
0 commit comments