Package me.croabeast.common.time
Class TimeFormatter
java.lang.Object
me.croabeast.common.time.TimeFormatter
Formats a duration given in seconds into a human-readable string using configurable time unit formats.
TimeFormatter uses a TimeValues instance to format time durations into strings.
It calculates the number of years, months, weeks, days, hours, minutes, and seconds contained within
a duration, and returns a formatted string where each non-zero unit is included with proper pluralization.
The resulting string is also colorized via the provided TakionLib instance.
Constants representing the number of seconds in each time unit are provided:
MINUTE: 60 secondsHOUR: 3600 secondsDAY: 86400 secondsWEEK: 604800 secondsMONTH: 2592000 seconds (approx.)YEAR: 31536000 seconds (approx.)
Example usage:
TakionLib lib = TakionLib.fromPlugin(plugin);
TimeFormatter formatter = new TimeFormatter(lib, TimeValues.DEFAULT_KEYS, 987654);
String formatted = formatter.formatTime(player);
System.out.println(formatted);
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intNumber of seconds in one day.static final intNumber of seconds in one hour.static final intNumber of seconds in one minute.static final intApproximate number of seconds in one month.static final intNumber of seconds in one week.static final intApproximate number of seconds in one year. -
Constructor Summary
ConstructorsConstructorDescriptionTimeFormatter(TakionLib lib, long seconds) Constructs a newTimeFormatterwith the given library instance and duration in seconds, using default time value formats.TimeFormatter(TakionLib lib, TimeValues values, long seconds) Constructs a newTimeFormatterwith the given library instance, time value formats, and duration in seconds. -
Method Summary
Modifier and TypeMethodDescriptionFormats the time duration into a human-readable string without any player context for colorization.formatTime(org.bukkit.entity.Player player) Formats the time duration into a human-readable string using the same player as both target and parser.formatTime(org.bukkit.entity.Player target, org.bukkit.entity.Player parser) Formats the time duration into a human-readable string.
-
Field Details
-
MINUTE
public static final int MINUTENumber of seconds in one minute.- See Also:
-
HOUR
public static final int HOURNumber of seconds in one hour.- See Also:
-
DAY
public static final int DAYNumber of seconds in one day.- See Also:
-
WEEK
public static final int WEEKNumber of seconds in one week.- See Also:
-
MONTH
public static final int MONTHApproximate number of seconds in one month.- See Also:
-
YEAR
public static final int YEARApproximate number of seconds in one year.- See Also:
-
-
Constructor Details
-
TimeFormatter
Constructs a newTimeFormatterwith the given library instance, time value formats, and duration in seconds.- Parameters:
lib- theTakionLibinstance used for colorization and formatting utilities (must not benull)values- theTimeValuesinstance containing format strings; ifnull, default keys are usedseconds- the duration in seconds to format
-
TimeFormatter
Constructs a newTimeFormatterwith the given library instance and duration in seconds, using default time value formats.- Parameters:
lib- theTakionLibinstance used for colorization and formatting utilitiesseconds- the duration in seconds to format
-
-
Method Details
-
formatTime
Formats the time duration into a human-readable string.The method decomposes the total seconds into years, months, weeks, days, hours, minutes, and seconds, concatenates the non-zero components using the configured splitter, and colorizes the final string.
- Parameters:
target- the player to whom the formatted time is targeted (for colorization; may benull)parser- the player context used for colorization (may benull)- Returns:
- the formatted time string
-
formatTime
Formats the time duration into a human-readable string using the same player as both target and parser.- Parameters:
player- the player to use for both target and parsing context for colorization- Returns:
- the formatted time string
-
formatTime
Formats the time duration into a human-readable string without any player context for colorization.- Returns:
- the formatted time string
-