1+ package dev .lars .utilsmanager .utils ;
2+
3+ import java .time .Duration ;
4+ import java .time .Instant ;
5+ import java .util .regex .Matcher ;
6+ import java .util .regex .Pattern ;
7+
8+ public class TimeUtil {
9+
10+ private static final Pattern TIME_PATTERN = Pattern .compile ("^(\\ d+)([smhd])$" );
11+
12+ public static Instant parseTimeToInstant (String timeString ) {
13+ if (timeString == null || timeString .isBlank ()) {
14+ throw new IllegalArgumentException ("Time string cannot be null or empty" );
15+ }
16+
17+ Matcher matcher = TIME_PATTERN .matcher (timeString .toLowerCase ().trim ());
18+ if (!matcher .matches ()) {
19+ return null ;
20+ }
21+
22+ long amount = Long .parseLong (matcher .group (1 ));
23+ String unit = matcher .group (2 );
24+
25+ Duration duration = switch (unit ) {
26+ case "s" -> Duration .ofSeconds (amount );
27+ case "m" -> Duration .ofMinutes (amount );
28+ case "h" -> Duration .ofHours (amount );
29+ case "d" -> Duration .ofDays (amount );
30+ default -> null ;
31+ };
32+
33+ if (duration == null ) {
34+ return null ;
35+ }
36+
37+ return Instant .now ().plus (duration );
38+ }
39+
40+ public static long parseTimeToMillis (String timeString ) {
41+ if (timeString == null || timeString .isBlank ()) {
42+ throw new IllegalArgumentException ("Time string cannot be null or empty" );
43+ }
44+
45+ Matcher matcher = TIME_PATTERN .matcher (timeString .toLowerCase ().trim ());
46+ if (!matcher .matches ()) {
47+ throw new IllegalArgumentException (
48+ "Invalid time format: '" + timeString + "'. Expected format: number followed by s/m/h/d"
49+ );
50+ }
51+
52+ long amount = Long .parseLong (matcher .group (1 ));
53+ String unit = matcher .group (2 );
54+
55+ return switch (unit ) {
56+ case "s" -> amount * 1000 ;
57+ case "m" -> amount * 60 * 1000 ;
58+ case "h" -> amount * 60 * 60 * 1000 ;
59+ case "d" -> amount * 24 * 60 * 60 * 1000 ;
60+ default -> throw new IllegalArgumentException ("Unknown time unit: " + unit );
61+ };
62+ }
63+
64+ public static long parseTimeToTicks (String timeString ) {
65+ return parseTimeToMillis (timeString ) / 50 ;
66+ }
67+
68+ public static Duration parseTimeToDuration (String timeString ) {
69+ if (timeString == null || timeString .isBlank ()) {
70+ throw new IllegalArgumentException ("Time string cannot be null or empty" );
71+ }
72+
73+ Matcher matcher = TIME_PATTERN .matcher (timeString .toLowerCase ().trim ());
74+ if (!matcher .matches ()) {
75+ throw new IllegalArgumentException (
76+ "Invalid time format: '" + timeString + "'. Expected format: number followed by s/m/h/d"
77+ );
78+ }
79+
80+ long amount = Long .parseLong (matcher .group (1 ));
81+ String unit = matcher .group (2 );
82+
83+ return switch (unit ) {
84+ case "s" -> Duration .ofSeconds (amount );
85+ case "m" -> Duration .ofMinutes (amount );
86+ case "h" -> Duration .ofHours (amount );
87+ case "d" -> Duration .ofDays (amount );
88+ default -> throw new IllegalArgumentException ("Unknown time unit: " + unit );
89+ };
90+ }
91+ }
0 commit comments