Class TimeStamp

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

public final class TimeStamp
extends java.lang.Object
implements java.lang.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/04/2020
Author:
Sven Maerivoet
  • Constructor Summary

    Constructors
    Constructor Description
    TimeStamp()
    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, java.time.ZoneId timeZoneID)
    Constructs a TimeStamp object corresponding to the specified Unix time (number of milliseconds since the epoch) using a specified time zone.
    TimeStamp​(java.lang.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 clear()
    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, java.time.ZoneId timeZoneID)
    Converts this TimeStamp object from a Unix time (number of milliseconds since the epoch) using a specified time zone.
    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.
    long convertToUnixTime​(DateStamp date, java.time.ZoneId timeZoneID)
    Converts this TimeStamp object to a Unix time (number of milliseconds since the epoch) using a specified date and time zone.
    boolean equals​(java.lang.Object object)
    Default equals() operator.
    java.lang.String getHMSMsString()
    Returns a String representation of this TimeStamp object in the format HH:mm:ss.SSS.
    java.lang.String getHMSString()
    Returns a String representation of this TimeStamp object in the format HH:mm:ss.
    java.lang.String getHMString()
    Returns a String representation of this TimeStamp object in the format HH:mm.
    int getHour()
    Returns this TimeStamp's hour of the day [0-23].
    int getMillisecond()
    Returns this TimeStamp's millisecond [0-999].
    int getMillisecondOfDay()
    Returns this TimeStamp's millisecond-of-day (since 00:00:00.000).
    int getMinute()
    Returns this TimeStamp's minute [0-59].
    int getMinuteOfDay()
    Returns this TimeStamp's minute-of-day (since 00:00:00.000).
    java.lang.String getMSMsString()
    Returns a String representation of this TimeStamp object in the format mm:ss.SSS.
    java.lang.String getMSString()
    Returns a String representation of this TimeStamp object in the format mm:ss.
    int getSecond()
    Returns this TimeStamp's second [0-59].
    int getSecondOfDay()
    Returns this TimeStamp's second-of-day (since 00:00:00.000).
    java.lang.String getSMsString()
    Returns a String representation of this TimeStamp object in the format ss.SSS.
    java.lang.String getSString()
    Returns a String representation of this TimeStamp object in the format ss.
    int hashCode()  
    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​(java.lang.String timeString)
    Sets this TimeStamp object corresponding to the specified string representation.
    void setToHMS​(java.lang.String timeString)
    Sets this TimeStamp object corresponding to the specified string representation.
    void setToHMSMs​(java.lang.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 setToNow()
    Sets this TimeStamp to the current system time.
    java.lang.String toString()
    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​(java.lang.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:
      SimpleDateFormat
    • 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, java.time.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​(java.lang.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:
      SimpleDateFormat
    • setToHMS

      public void setToHMS​(java.lang.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:
      SimpleDateFormat
    • setToHMSMs

      public void setToHMSMs​(java.lang.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:
      SimpleDateFormat
    • 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 java.lang.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:
      SimpleDateFormat
    • getHMSString

      public java.lang.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:
      SimpleDateFormat
    • getHMString

      public java.lang.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:
      SimpleDateFormat
    • getMSString

      public java.lang.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:
      SimpleDateFormat
    • getMSMsString

      public java.lang.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:
      SimpleDateFormat
    • getSMsString

      public java.lang.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:
      SimpleDateFormat
    • getSString

      public java.lang.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:
      SimpleDateFormat
    • convertFromUnixTime

      public void convertFromUnixTime​(long unixMilliseconds, java.time.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, java.time.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)
    • 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 java.lang.Comparable<TimeStamp>
    • toString

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

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

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