Class Timestring

java.lang.Object
tech.guilhermekaua.spigotboot.core.utils.Timestring

public final class Timestring extends Object
A utility class for parsing duration strings into numeric values.
  • Constructor Details

    • Timestring

      public Timestring()
  • Method Details

    • duration

      public static double duration(@NotNull @NotNull String str, @Nullable @Nullable String format)
      Convert a duration string to the specified time unit.

      Examples:

      • duration("5m 30s", "s") returns 330.0
      • duration("1h30m", "m") returns 90.0
      • duration("2.5d", "h") returns 60.0
      • duration("1e3ms", "s") returns 1.0
      Parameters:
      str - the string to parse (e.g., "5m 30s", "1h30m", "2.5d")
      format - the unit to return the result in (e.g., "s", "ms", "h"). If null, returns milliseconds.
      Returns:
      the parsed value in the specified format, or -1 if parsing fails
    • durationLong

      public static long durationLong(@NotNull @NotNull String str, @Nullable @Nullable String format)
      Convert a duration string to the specified time unit as a long.
      Parameters:
      str - the string to parse
      format - the unit to return the result in
      Returns:
      the parsed value in the specified format, or -1 if parsing fails