Class Time

java.lang.Object
dev.demeng.pluginbase.Time

public final class Time extends Object
Utilities for time/duration parsing and formatting.
  • Field Details

    • DATE_TIME_FORMAT

      public static final ThreadLocal<DateFormat> DATE_TIME_FORMAT
      The date format used for dates and times combined.
    • DATE_FORMAT

      public static final ThreadLocal<DateFormat> DATE_FORMAT
      The date format used for dates.
  • Constructor Details

    • Time

      public Time()
  • Method Details

    • formatDateTime

      public static String formatDateTime(long time)
      Formats the date and time using DATE_TIME_FORMAT.
      Parameters:
      time - The time to format
      Returns:
      The formatted date and time
    • formatDate

      public static String formatDate(long time)
      Formats the date using DATE_FORMAT.
      Parameters:
      time - The time to format
      Returns:
      The formatted date
    • parse

      @NotNull public static @NotNull Duration parse(@NotNull @NotNull String input) throws IllegalArgumentException
      Converts the given duration string into milliseconds and wraps it inside an Optional. Supported time units include years (y), months (mo), weeks (w), days (d), hours (h), minutes (m), and seconds (s).
      Parameters:
      input - The duration string
      Returns:
      The parsed duration
      Throws:
      IllegalArgumentException - If the input could not be parsed
    • parseSafely

      @NotNull public static @NotNull Optional<Duration> parseSafely(@NotNull @NotNull String input)
      Attempts to parse a Duration and returns the result as an Optional-wrapped object.
      Parameters:
      input - The input string
      Returns:
      An Optional Duration
      See Also:
    • formatDuration

      public static String formatDuration(Time.DurationFormatter formatter, long duration)
      Formats the duration using the selected formatter.
      Parameters:
      formatter - The formatter to use
      duration - The duration to format
      Returns:
      The formatted duration
    • toSqlTimestamp

      public static Timestamp toSqlTimestamp(long timestamp)
      Converts the long timestamp into an SQL timestamp.
      Parameters:
      timestamp - The long timestamp
      Returns:
      The equivalent timestamp, as one used by SQL
    • fromSqlTimestamp

      public static long fromSqlTimestamp(@NotNull @NotNull String timestamp)
      Converts the SQL timestamp into a long.
      Parameters:
      timestamp - The SQL timestamp
      Returns:
      The equivalent timestamp, as a long