Class DurationFormat

java.lang.Object
com.enhancedechest.util.DurationFormat

public final class DurationFormat extends Object
Parses and formats human-friendly durations.

Accepted input units: s (second), m (minute), h (hour), d (day), w (week), mo (month) and y (year). A month is approximated as 30 days and a year as 365 days. Simple values look like 20s, 5m or 1h; complex values join several components with underscores, e.g. 1d_2h_30m_15s.

All methods are pure and side-effect free, which keeps them unit-testable.

  • Method Details

    • parse

      public static long parse(String input)
      Parses a duration string into milliseconds.

      Examples: 20s, 5m, 1h, 1d_2h_30m_15s.

      Parameters:
      input - the duration string (case-insensitive, components separated by _)
      Returns:
      the duration in milliseconds (always positive)
      Throws:
      IllegalArgumentException - if the string is null, empty or malformed
    • remainingParts

      public static List<DurationFormat.Part> remainingParts(long millis)
      Breaks a remaining duration into the two most significant non-zero units, e.g. [1d, 4h] or [23h, 45m], as locale-free DurationFormat.Parts the caller renders into localized text. Rather than baking unit suffixes into a string (which could not be localized), this returns only the numbers and unit names.

      Edge cases mirror the old compact formatter: a zero or negative duration yields a single 0 second part, and a sub-second positive value rounds up to a single 1 second part — so the result is never empty.

      Parameters:
      millis - the remaining time in milliseconds
      Returns:
      an immutable list of one or two parts, most significant first
    • formatRemaining

      public static String formatRemaining(long millis)
      Compact, non-localized form of remainingParts(long) ("1d 4h", "0s") for logs and the startup banner. Player-facing text must localize the parts instead (see LanguageManager#duration), since these suffixes are English.
      Parameters:
      millis - the remaining time in milliseconds
      Returns:
      a short label; "0s" when the duration is zero or negative