Enum TimeFormat

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<TimeFormat>

    public enum TimeFormat
    extends java.lang.Enum<TimeFormat>
    Utility enum used to provide different markdown styles for timestamps.
    These can be used to represent a unix epoch timestamp in different formats.

    These timestamps are rendered by the individual receiving discord client in a local timezone and language format. Each timestamp can be displayed with different TimeFormats.

    Example

    
     channel.sendMessage("Current Time: " + TimeFormat.RELATIVE.now()).queue();
     channel.sendMessage("Uptime: " + TimeFormat.RELATIVE.format(getStartTime())).queue();
     
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
      DATE_LONG
      Formats date as 16 June 2021
      DATE_SHORT
      Formats date as 16/06/2021 or 06/16/2021
      DATE_TIME_LONG
      Formats date and time as Wednesday, 16 June 2021 18:49 or Wednesday, June 16, 2021 6:49 PM
      DATE_TIME_SHORT
      Formats date and time as 16 June 2021 18:49 or June 16, 2021 6:49 PM
      RELATIVE
      Formats date and time as relative 18 minutes ago or 2 days ago
      TIME_LONG
      Formats time as 18:49:26 or 6:49:26 PM
      TIME_SHORT
      Formats time as 18:49 or 6:49 PM
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static TimeFormat DEFAULT
      The default time format used when no style is provided.
      static java.util.regex.Pattern MARKDOWN
      Pattern used for parse(String).
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Timestamp after​(long millis)
      Shortcut for style.now().plus(millis).
      Timestamp after​(java.time.Duration duration)
      Shortcut for style.now().plus(duration).
      Timestamp atInstant​(java.time.Instant instant)
      Converts the provided Instant into a Timestamp with this style.
      Timestamp atTimestamp​(long timestamp)
      Converts the provided unix epoch timestamp into a Timestamp with this style.
      Timestamp before​(long millis)
      Shortcut for style.now().minus(millis).
      Timestamp before​(java.time.Duration duration)
      Shortcut for style.now().minus(duration).
      java.lang.String format​(long timestamp)
      Formats the provided unix epoch timestamp into a timestamp markdown.
      java.lang.String format​(java.time.temporal.TemporalAccessor temporal)
      Formats the provided TemporalAccessor instance into a timestamp markdown.
      static TimeFormat fromStyle​(java.lang.String style)
      Returns the time format for the provided style flag.
      java.lang.String getStyle()
      The display style flag used for the markdown representation.
      Timestamp now()
      Shortcut for style.atTimestamp(System.currentTimeMillis()).
      static Timestamp parse​(java.lang.String markdown)
      Parses the provided markdown into a Timestamp instance.
      static TimeFormat valueOf​(java.lang.String name)
      Returns the enum constant of this type with the specified name.
      static TimeFormat[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      • Methods inherited from class java.lang.Enum

        compareTo, equals, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Enum Constant Detail

      • TIME_SHORT

        public static final TimeFormat TIME_SHORT
        Formats time as 18:49 or 6:49 PM
      • TIME_LONG

        public static final TimeFormat TIME_LONG
        Formats time as 18:49:26 or 6:49:26 PM
      • DATE_SHORT

        public static final TimeFormat DATE_SHORT
        Formats date as 16/06/2021 or 06/16/2021
      • DATE_LONG

        public static final TimeFormat DATE_LONG
        Formats date as 16 June 2021
      • DATE_TIME_SHORT

        public static final TimeFormat DATE_TIME_SHORT
        Formats date and time as 16 June 2021 18:49 or June 16, 2021 6:49 PM
      • DATE_TIME_LONG

        public static final TimeFormat DATE_TIME_LONG
        Formats date and time as Wednesday, 16 June 2021 18:49 or Wednesday, June 16, 2021 6:49 PM
      • RELATIVE

        public static final TimeFormat RELATIVE
        Formats date and time as relative 18 minutes ago or 2 days ago
    • Field Detail

      • DEFAULT

        public static final TimeFormat DEFAULT
        The default time format used when no style is provided.
      • MARKDOWN

        public static final java.util.regex.Pattern MARKDOWN
        Pattern used for parse(String).

        Groups

        Javadoc is stupid, this is not a required tag
        Index Name Description
        0 N/A The entire timestamp markdown
        1 time The timestamp value as a unix epoch in second precision
        2 style The style used for displaying the timestamp (single letter flag)
        See Also:
        parse(String)
    • Method Detail

      • values

        public static TimeFormat[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (TimeFormat c : TimeFormat.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static TimeFormat valueOf​(java.lang.String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
        java.lang.NullPointerException - if the argument is null
      • getStyle

        @Nonnull
        public java.lang.String getStyle()
        The display style flag used for the markdown representation.
        This is encoded into the markdown to provide the client with rendering context.
        Returns:
        The style flag
      • fromStyle

        @Nonnull
        public static TimeFormat fromStyle​(@Nonnull
                                           java.lang.String style)
        Returns the time format for the provided style flag.
        Parameters:
        style - The style flag
        Returns:
        The representative TimeFormat or DEFAULT if none could be identified
        Throws:
        java.lang.IllegalArgumentException - If the provided style string is not exactly one character long
      • parse

        @Nonnull
        public static Timestamp parse​(@Nonnull
                                      java.lang.String markdown)
        Parses the provided markdown into a Timestamp instance.
        This is the reverse operation for the Timestamp.toString() representation.
        Parameters:
        markdown - The markdown for the timestamp value
        Returns:
        Timestamp instance for the provided markdown
        Throws:
        java.lang.IllegalArgumentException - If the provided markdown is null or does not match the MARKDOWN pattern
      • format

        @Nonnull
        public java.lang.String format​(@Nonnull
                                       java.time.temporal.TemporalAccessor temporal)
        Formats the provided TemporalAccessor instance into a timestamp markdown.
        Parameters:
        temporal - The TemporalAccessor
        Returns:
        The markdown string with this encoded style
        Throws:
        java.lang.IllegalArgumentException - If the provided temporal instance is null
        java.time.DateTimeException - If the temporal accessor cannot be converted to an instant
        See Also:
        Instant.from(TemporalAccessor)
      • format

        @Nonnull
        public java.lang.String format​(long timestamp)
        Formats the provided unix epoch timestamp into a timestamp markdown.
        Compatible with millisecond precision timestamps such as the ones provided by System.currentTimeMillis().
        Parameters:
        timestamp - The millisecond epoch
        Returns:
        The markdown string with this encoded style
      • atInstant

        @Nonnull
        public Timestamp atInstant​(@Nonnull
                                   java.time.Instant instant)
        Converts the provided Instant into a Timestamp with this style.
        Parameters:
        instant - The Instant for the timestamp
        Returns:
        The Timestamp instance
        Throws:
        java.lang.IllegalArgumentException - If null is provided
        See Also:
        now(), atTimestamp(long), Instant.from(TemporalAccessor), Instant.toEpochMilli()
      • atTimestamp

        @Nonnull
        public Timestamp atTimestamp​(long timestamp)
        Converts the provided unix epoch timestamp into a Timestamp with this style.
        Compatible with millisecond precision timestamps such as the ones provided by System.currentTimeMillis().
        Parameters:
        timestamp - The millisecond epoch
        Returns:
        The Timestamp instance
        See Also:
        now()
      • after

        @Nonnull
        public Timestamp after​(@Nonnull
                               java.time.Duration duration)
        Shortcut for style.now().plus(duration).
        Parameters:
        duration - The Duration offset into the future
        Returns:
        Timestamp instance for the offset relative to the current time
        Throws:
        java.lang.IllegalArgumentException - If null is provided
        See Also:
        now(), Timestamp.plus(Duration)
      • after

        @Nonnull
        public Timestamp after​(long millis)
        Shortcut for style.now().plus(millis).
        Parameters:
        millis - The millisecond offset into the future
        Returns:
        Timestamp instance for the offset relative to the current time
        See Also:
        now(), Timestamp.plus(long)
      • before

        @Nonnull
        public Timestamp before​(@Nonnull
                                java.time.Duration duration)
        Shortcut for style.now().minus(duration).
        Parameters:
        duration - The Duration offset into the past
        Returns:
        Timestamp instance for the offset relative to the current time
        Throws:
        java.lang.IllegalArgumentException - If null is provided
        See Also:
        now(), Timestamp.minus(Duration)
      • before

        @Nonnull
        public Timestamp before​(long millis)
        Shortcut for style.now().minus(millis).
        Parameters:
        millis - The millisecond offset into the past
        Returns:
        Timestamp instance for the offset relative to the current time
        See Also:
        now(), Timestamp.minus(long)