Class TimeFormatter

java.lang.Object
me.croabeast.common.time.TimeFormatter

public class TimeFormatter extends Object
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 seconds
  • HOUR: 3600 seconds
  • DAY: 86400 seconds
  • WEEK: 604800 seconds
  • MONTH: 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

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Number of seconds in one day.
    static final int
    Number of seconds in one hour.
    static final int
    Number of seconds in one minute.
    static final int
    Approximate number of seconds in one month.
    static final int
    Number of seconds in one week.
    static final int
    Approximate number of seconds in one year.
  • Constructor Summary

    Constructors
    Constructor
    Description
    TimeFormatter(TakionLib lib, long seconds)
    Constructs a new TimeFormatter with the given library instance and duration in seconds, using default time value formats.
    TimeFormatter(TakionLib lib, TimeValues values, long seconds)
    Constructs a new TimeFormatter with the given library instance, time value formats, and duration in seconds.
  • Method Summary

    Modifier and Type
    Method
    Description
    Formats 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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • TimeFormatter

      public TimeFormatter(TakionLib lib, TimeValues values, long seconds)
      Constructs a new TimeFormatter with the given library instance, time value formats, and duration in seconds.
      Parameters:
      lib - the TakionLib instance used for colorization and formatting utilities (must not be null)
      values - the TimeValues instance containing format strings; if null, default keys are used
      seconds - the duration in seconds to format
    • TimeFormatter

      public TimeFormatter(TakionLib lib, long seconds)
      Constructs a new TimeFormatter with the given library instance and duration in seconds, using default time value formats.
      Parameters:
      lib - the TakionLib instance used for colorization and formatting utilities
      seconds - the duration in seconds to format
  • Method Details

    • formatTime

      public String formatTime(org.bukkit.entity.Player target, org.bukkit.entity.Player parser)
      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 be null)
      parser - the player context used for colorization (may be null)
      Returns:
      the formatted time string
    • formatTime

      public String formatTime(org.bukkit.entity.Player player)
      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

      public String formatTime()
      Formats the time duration into a human-readable string without any player context for colorization.
      Returns:
      the formatted time string