Class TimeStamp

java.lang.Object
org.sm.smtools.util.TimeStamp
All Implemented Interfaces:
Comparable<TimeStamp>

public final class TimeStamp extends Object implements Comparable<TimeStamp>
The TimeStamp class acts as a container for time stamps.

A time stamp is defined by its hour, minute, second and millisecond.

Note that this class cannot be subclassed!

Version:
03/08/2022
Author:
Sven Maerivoet
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a TimeStamp object corresponding to the current system time.
    TimeStamp(int hour, int minute, int second)
    Constructs a TimeStamp object corresponding to the specified values.
    TimeStamp(int hour, int minute, int second, int millisecond)
    Constructs a TimeStamp object corresponding to the specified values.
    TimeStamp(long unixMilliseconds)
    Constructs a TimeStamp object corresponding to the specified Unix time (number of milliseconds since the epoch) using the user's local time zone.
    TimeStamp(long unixMilliseconds, ZoneId timeZoneID)
    Constructs a TimeStamp object corresponding to the specified Unix time (number of milliseconds since the epoch) using a specified time zone.
    TimeStamp(String timeString)
    Constructs a TimeStamp object corresponding to the specified string representation.
    TimeStamp(TimeStamp timeStamp)
    Constructs a TimeStamp object as a copy of another TimeStamp object.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Sets this TimeStamp to 00:00:00.000.
    int
    compareTo(TimeStamp otherTimeStamp)
    The overloaded Comparable interface.
    void
    convertFromUnixTime(long unixMilliseconds)
    Converts this TimeStamp object from a Unix time (number of milliseconds since the epoch) using the user's local time zone.
    void
    convertFromUnixTime(long unixMilliseconds, ZoneId timeZoneID)
    Converts this TimeStamp object from a Unix time (number of milliseconds since the epoch) using a specified time zone.
    long
    Converts this TimeStamp object to a Unix time (number of milliseconds since the epoch) using the user's date and local time zone.
    long
    convertToUnixTime(DateStamp date, ZoneId timeZoneID)
    Converts this TimeStamp object to a Unix time (number of milliseconds since the epoch) using a specified date and time zone.
    static void
    convertToUTC(String dateTimeStampStr, DateStamp dateStamp, TimeStamp timeStamp)
    Converts the given date- and time description into UTC-zoned date- and timestamps.
    The description has the format of YYYY-MM-DDTHH:MM:SS+|-HH:MM
    static void
    convertToUTC(DateStamp dateStamp, TimeStamp timeStamp, String zoneStr)
    Converts the given date- and timestamps to UTC, taking a specified time zone into account.
    boolean
    equals(Object object)
    Default equals() operator.
    Returns a String representation of this TimeStamp object in the format HH:mm:ss.SSS.
    Returns a String representation of this TimeStamp object in the format HH:mm:ss.
    Returns a String representation of this TimeStamp object in the format HH:mm.
    int
    Returns this TimeStamp's hour of the day [0-23].
    int
    Returns this TimeStamp's millisecond [0-999].
    int
    Returns this TimeStamp's millisecond-of-day (since 00:00:00.000).
    int
    Returns this TimeStamp's minute [0-59].
    int
    Returns this TimeStamp's minute-of-day (since 00:00:00.000).
    Returns a String representation of this TimeStamp object in the format mm:ss.SSS.
    Returns a String representation of this TimeStamp object in the format mm:ss.
    int
    Returns this TimeStamp's second [0-59].
    int
    Returns this TimeStamp's second-of-day (since 00:00:00.000).
    Returns a String representation of this TimeStamp object in the format ss.SSS.
    Returns a String representation of this TimeStamp object in the format ss.
    int
     
    void
    set(int hour, int minute, int second, int millisecond)
    Sets this TimeStamp to the specified values.
    void
    set(TimeStamp timeStamp)
    Sets this TimeStamp equal to another one.
    void
    setToHM(String timeString)
    Sets this TimeStamp object corresponding to the specified string representation.
    void
    setToHMS(String timeString)
    Sets this TimeStamp object corresponding to the specified string representation.
    void
    setToHMSMs(String timeString)
    Sets this TimeStamp object corresponding to the specified string representation.
    void
    setToMillisecondOfDay(long millisecond)
    Sets this TimeStamp based on a cumulative millisecond representation (since 00:00:00.000).
    void
    Sets this TimeStamp to the current system time.
    Default overloaded toString() method.

    Methods inherited from class java.lang.Object

    clone, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • TimeStamp

      public TimeStamp()
      Constructs a TimeStamp object corresponding to the current system time.
    • TimeStamp

      public TimeStamp(int hour, int minute, int second)
      Constructs a TimeStamp object corresponding to the specified values.

      The millisecond is set to zero.

      Parameters:
      hour - the hour component
      minute - the minute component
      second - the second component
    • TimeStamp

      public TimeStamp(int hour, int minute, int second, int millisecond)
      Constructs a TimeStamp object corresponding to the specified values.
      Parameters:
      hour - the hour component
      minute - the minute component
      second - the second component
      millisecond - the millisecond component
    • TimeStamp

      public TimeStamp(String timeString) throws DateTimeFormatException
      Constructs a TimeStamp object corresponding to the specified string representation.

      The string has to have the following specific format: HH:mm:ss, e.g., 12:45:16

      Parameters:
      timeString - the string representation of the time stamp (in the format HH:mm:ss)
      Throws:
      DateTimeFormatException - if an error occurred during conversion
      See Also:
    • TimeStamp

      public TimeStamp(long unixMilliseconds)
      Constructs a TimeStamp object corresponding to the specified Unix time (number of milliseconds since the epoch) using the user's local time zone. The Unix time typically looks like, e.g., 1563319245000L.
      Parameters:
      unixMilliseconds - the Unix time (number of milliseconds since the epoch)
    • TimeStamp

      public TimeStamp(long unixMilliseconds, ZoneId timeZoneID)
      Constructs a TimeStamp object corresponding to the specified Unix time (number of milliseconds since the epoch) using a specified time zone. The Unix time typically looks like, e.g., 1563319245000L.
      Parameters:
      unixMilliseconds - the Unix time (number of milliseconds since the epoch)
      timeZoneID - the ID of the time zone
    • TimeStamp

      public TimeStamp(TimeStamp timeStamp)
      Constructs a TimeStamp object as a copy of another TimeStamp object.

      This is the copy constructor.

      Parameters:
      timeStamp - the TimeStamp object to deep copy
  • Method Details

    • clear

      public void clear()
      Sets this TimeStamp to 00:00:00.000.
    • set

      public void set(int hour, int minute, int second, int millisecond)
      Sets this TimeStamp to the specified values.
      Parameters:
      hour - the hour component
      minute - the minute component
      second - the second component
      millisecond - the millisecond component
    • set

      public void set(TimeStamp timeStamp)
      Sets this TimeStamp equal to another one.
      Parameters:
      timeStamp - the TimeStamp to copy
    • setToMillisecondOfDay

      public void setToMillisecondOfDay(long millisecond)
      Sets this TimeStamp based on a cumulative millisecond representation (since 00:00:00.000).
      Parameters:
      millisecond - the number of milliseconds (since 00:00:00.000) to convert to a TimeStamp object
    • setToHM

      public void setToHM(String timeString) throws DateTimeFormatException
      Sets this TimeStamp object corresponding to the specified string representation.

      The string has to have the following specific format: HH:mm, e.g., 10:15

      Parameters:
      timeString - the string representation of the time stamp (in the format HH:mm)
      Throws:
      DateTimeFormatException - if an error occurred during conversion
      See Also:
    • setToHMS

      public void setToHMS(String timeString) throws DateTimeFormatException
      Sets this TimeStamp object corresponding to the specified string representation.

      The string has to have the following specific format: HH:mm:ss, e.g., 12:45:16 or 8:32:48

      Parameters:
      timeString - the string representation of the time stamp (in the format HH:mm:ss)
      Throws:
      DateTimeFormatException - if an error occurred during conversion
      See Also:
    • setToHMSMs

      public void setToHMSMs(String timeString) throws DateTimeFormatException
      Sets this TimeStamp object corresponding to the specified string representation.

      The string has to have the following specific format: HH:mm:ss.mls, e.g., 12:45:16.002

      Parameters:
      timeString - the string representation of the time stamp (in the format HH:mm:ss.SSS)
      Throws:
      DateTimeFormatException - if an error occurred during conversion
      See Also:
    • setToNow

      public void setToNow()
      Sets this TimeStamp to the current system time.
    • getHour

      public int getHour()
      Returns this TimeStamp's hour of the day [0-23].
      Returns:
      the hour
    • getMinute

      public int getMinute()
      Returns this TimeStamp's minute [0-59].
      Returns:
      the minute
    • getSecond

      public int getSecond()
      Returns this TimeStamp's second [0-59].
      Returns:
      the second
    • getMillisecond

      public int getMillisecond()
      Returns this TimeStamp's millisecond [0-999].
      Returns:
      the millisecond
    • getMinuteOfDay

      public int getMinuteOfDay()
      Returns this TimeStamp's minute-of-day (since 00:00:00.000).
      Returns:
      the number of minutes (since 00:00:00:000) corresponding to this TimeStamp object
    • getSecondOfDay

      public int getSecondOfDay()
      Returns this TimeStamp's second-of-day (since 00:00:00.000).
      Returns:
      the number of seconds (since 00:00:00:000) corresponding to this TimeStamp object
    • getMillisecondOfDay

      public int getMillisecondOfDay()
      Returns this TimeStamp's millisecond-of-day (since 00:00:00.000).
      Returns:
      the number of milliseconds (since 00:00:00:000) corresponding to this TimeStamp object
    • getHMSMsString

      public String getHMSMsString()
      Returns a String representation of this TimeStamp object in the format HH:mm:ss.SSS.
      Returns:
      a String representation of this TimeStamp object in the format HH:mm:ss.SSS
      See Also:
    • getHMSString

      public String getHMSString()
      Returns a String representation of this TimeStamp object in the format HH:mm:ss.
      Returns:
      a String representation of this TimeStamp object in the format HH:mm:ss
      See Also:
    • getHMString

      public String getHMString()
      Returns a String representation of this TimeStamp object in the format HH:mm.
      Returns:
      a String representation of this TimeStamp object in the format HH:mm
      See Also:
    • getMSString

      public String getMSString()
      Returns a String representation of this TimeStamp object in the format mm:ss.
      Returns:
      a String representation of this TimeStamp object in the format mm:ss
      See Also:
    • getMSMsString

      public String getMSMsString()
      Returns a String representation of this TimeStamp object in the format mm:ss.SSS.
      Returns:
      a String representation of this TimeStamp object in the format mm:ss.SSS
      See Also:
    • getSMsString

      public String getSMsString()
      Returns a String representation of this TimeStamp object in the format ss.SSS.
      Returns:
      a String representation of this TimeStamp object in the format ss.SSS
      See Also:
    • getSString

      public String getSString()
      Returns a String representation of this TimeStamp object in the format ss.
      Returns:
      a String representation of this TimeStamp object in the format ss
      See Also:
    • convertFromUnixTime

      public void convertFromUnixTime(long unixMilliseconds, ZoneId timeZoneID)
      Converts this TimeStamp object from a Unix time (number of milliseconds since the epoch) using a specified time zone. The Unix time typically looks like, e.g., 1563319245000L.
      Parameters:
      unixMilliseconds - the Unix time (number of milliseconds since the epoch)
      timeZoneID - the ID of the time zone
    • convertFromUnixTime

      public void convertFromUnixTime(long unixMilliseconds)
      Converts this TimeStamp object from a Unix time (number of milliseconds since the epoch) using the user's local time zone. The Unix time typically looks like, e.g., 1563319245000L.
      Parameters:
      unixMilliseconds - the Unix time (number of milliseconds since the epoch)
    • convertToUnixTime

      public long convertToUnixTime(DateStamp date, ZoneId timeZoneID)
      Converts this TimeStamp object to a Unix time (number of milliseconds since the epoch) using a specified date and time zone. The Unix time typically looks like, e.g., 1563319245000L.
      Parameters:
      date - the date at which the timestamp occurs
      timeZoneID - the ID of the time zone
      Returns:
      the Unix time (number of milliseconds since the epoch)
    • convertToUTC

      public static void convertToUTC(DateStamp dateStamp, TimeStamp timeStamp, String zoneStr)
      Converts the given date- and timestamps to UTC, taking a specified time zone into account.
      Parameters:
      dateStamp - the datestamp to use and convert
      timeStamp - the timestamp to use and convert
      zoneStr - the time zone
    • convertToUTC

      public static void convertToUTC(String dateTimeStampStr, DateStamp dateStamp, TimeStamp timeStamp)
      Converts the given date- and time description into UTC-zoned date- and timestamps.
      The description has the format of YYYY-MM-DDTHH:MM:SS+|-HH:MM
      Parameters:
      dateTimeStampStr - the date- and time description to use (YYYY-MM-DDTHH:MM:SS+HH:MM)
      dateStamp - the datestamp to store the result into
      timeStamp - the timestamp to store the result into
    • convertToUnixTime

      public long convertToUnixTime(DateStamp date)
      Converts this TimeStamp object to a Unix time (number of milliseconds since the epoch) using the user's date and local time zone. The Unix time typically looks like, e.g., 1563319245000L.
      Parameters:
      date - the date at which the timestamp occurs
      Returns:
      the Unix time (number of milliseconds since the epoch)
    • compareTo

      public int compareTo(TimeStamp otherTimeStamp)
      The overloaded Comparable interface.
      Specified by:
      compareTo in interface Comparable<TimeStamp>
    • toString

      public String toString()
      Default overloaded toString() method.
      Overrides:
      toString in class Object
      Returns:
      a String representation of this TimeStamp object in the format HH:mm:ss.SSS
      See Also:
    • equals

      public boolean equals(Object object)
      Default equals() operator.
      Overrides:
      equals in class Object
      Returns:
      true if both objects denote the same time stamp, false otherwise
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
      Returns:
      the object's unique hashcode