Class DateStamp

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

public final class DateStamp
extends java.lang.Object
implements java.lang.Comparable<DateStamp>
The DateStamp class acts as a container for date stamps.

A date stamp is defined by its year, month and day.

Note that a valid I18NL10N database must be available!

Note that this class cannot be subclassed!

Version:
03/03/2021
Author:
Sven Maerivoet
  • Constructor Summary

    Constructors
    Constructor Description
    DateStamp()
    Constructs a DateStamp object corresponding to the current system date.
    DateStamp​(int weekOfYear, int year)
    Constructs a DateStamp object corresponding to the specified values.
    DateStamp​(int day, int month, int year)
    Constructs a DateStamp object corresponding to the specified values.
    DateStamp​(long unixMilliseconds)
    Constructs a DateStamp object corresponding to the specified Unix time (number of milliseconds since the epoch) using the user's local time zone.
    DateStamp​(long unixMilliseconds, java.time.ZoneId timeZoneID)
    Constructs a DateStamp object corresponding to the specified Unix time (number of milliseconds since the epoch) using a specified time zone.
    DateStamp​(java.lang.String dateString, boolean... ymdFlag)
    Constructs a DateStamp object corresponding to the specified string representation.
    DateStamp​(DateStamp dateStamp)
    Constructs a DateStamp object as a copy of another DateStamp object.
  • Method Summary

    Modifier and Type Method Description
    int compareTo​(DateStamp otherDateStamp)
    The overloaded Comparable interface.
    void convertFromUnixTime​(long unixMilliseconds)
    Converts this DateStamp 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 DateStamp object from a Unix time (number of milliseconds since the epoch) using a specified time zone.
    long convertToUnixTime()
    Converts this DateStamp object to a Unix time (number of milliseconds since the epoch) using the user's local time zone.
    long convertToUnixTime​(java.time.ZoneId timeZoneID)
    Converts this DateStamp object to a Unix time (number of milliseconds since the epoch) using a specified time zone.
    boolean equals​(java.lang.Object object)
    Default equals() operator.
    int getDayOfMonth()
    Returns this DateStamp's day-of-month [1-31].
    int getDayOfWeek()
    Returns this DateStamp's day-of-week [1-7].
    static java.lang.String getDayOfWeekString​(int dayOfWeek)
    Helper method to convert a day-of-week index [1 (Monday) - 7 (Sunday)] to a weekday name.
    int getDayOfYear()
    Returns this DateStamp's day-of-year.
    java.lang.String getDMYString()
    Returns a String representation of this DateStamp object in the format dd/MM/yyyy.
    java.lang.String getFullDateString()
    Returns a String representation of this DateStamp object as a fully formatted string.
    int getMonth()
    Returns this DateStamp's month [1-12].
    static int getMonth​(java.lang.String month)
    Helper method to convert a month name to a month index [1-12].
    static java.lang.String getMonthString​(int month)
    Helper method to convert a month index [1-12] to a month name.
    int getNumberOfDaysInMonth()
    Returns the number of days in this DateStamp's month.
    static int getNumberOfDaysInMonth​(int month, int year)
    Returns the number of days in a specified month [1-12] and a given year.
    int getWeekOfYear()
    Returns this DateStamp's week-of-year [1-52].
    int getYear()
    Returns this DateStamp's year.
    java.lang.String getYMDString()
    Returns a String representation of this DateStamp object in the format yyyy-MM-dd.
    static java.time.ZoneId getZoneID​(java.lang.String timeZoneShortDescription)
    Helper method to convert a time zone's short description into a ZoneId object.
    int hashCode()  
    boolean isLeapYear()
    Checks whether or not this DateStamp's year is a leap year (assuming a Gregorian calendar).
    boolean isWeekend()
    Returns whether or not the current DateStamp is in a weekend.
    void set​(DateStamp dateStamp)
    Sets this DateStamp equal to another one.
    void setToDMY​(int day, int month, int year)
    Sets this DateStamp to the specified values.
    void setToDMY​(java.lang.String dateString)
    Sets this DateStamp object corresponding to the specified string representation.
    void setToNextDay​(int nrOfNextDays)
    Sets this DateStamp to the next day.
    void setToNextMonth​(int nrOfNextMonths)
    Sets this DateStamp to the next month.
    void setToNextYear​(int nrOfNextYears)
    Sets this DateStamp to the next year.
    void setToNow()
    Sets this DateStamp to the current system date.
    void setToPreviousDay​(int nrOfPreviousDays)
    Sets this DateStamp to the previous day.
    void setToPreviousMonth​(int nrOfPreviousMonths)
    Sets this DateStamp to the previous month.
    void setToPreviousYear​(int nrOfPreviousYears)
    Sets this DateStamp to the previous year.
    void setToWoYY​(int weekOfYear, int year)
    Sets this DateStamp to the specified values.
    void setToYMD​(java.lang.String dateString)
    Sets this DateStamp object corresponding to another string representation.
    java.lang.String toString()
    Default overloaded toString() method.

    Methods inherited from class java.lang.Object

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

    • DateStamp

      public DateStamp()
      Constructs a DateStamp object corresponding to the current system date.
    • DateStamp

      public DateStamp​(int day, int month, int year)
      Constructs a DateStamp object corresponding to the specified values.
      Parameters:
      day - the day component
      month - the month component
      year - the year component
    • DateStamp

      public DateStamp​(int weekOfYear, int year)
      Constructs a DateStamp object corresponding to the specified values.
      Parameters:
      weekOfYear - the week of the year component
      year - the year component
    • DateStamp

      public DateStamp​(java.lang.String dateString, boolean... ymdFlag)
      Constructs a DateStamp object corresponding to the specified string representation.

      The string has to have the following specific format: dd/MM/yyyy, e.g., 11/04/1976, or yyyy-MM-dd in case the optional boolean flag is set to true.

      Parameters:
      dateString - the string representation of the date stamp (in the format dd/MM/yyyy)
      ymdFlag - an optional boolean indicating if the format is to be interpreted as yyyy-MM-dd
    • DateStamp

      public DateStamp​(long unixMilliseconds)
      Constructs a DateStamp 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)
    • DateStamp

      public DateStamp​(long unixMilliseconds, java.time.ZoneId timeZoneID)
      Constructs a DateStamp 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
    • DateStamp

      public DateStamp​(DateStamp dateStamp)
      Constructs a DateStamp object as a copy of another DateStamp object.

      This is the copy constructor.

      Parameters:
      dateStamp - the DateStamp object to deep copy
  • Method Details

    • setToDMY

      public void setToDMY​(int day, int month, int year)
      Sets this DateStamp to the specified values.
      Parameters:
      day - the day component
      month - the month component
      year - the year component
    • setToWoYY

      public void setToWoYY​(int weekOfYear, int year)
      Sets this DateStamp to the specified values.
      Parameters:
      weekOfYear - the week of the year component
      year - the year component
    • set

      public void set​(DateStamp dateStamp)
      Sets this DateStamp equal to another one.
      Parameters:
      dateStamp - the DateStamp to copy
    • setToDMY

      public void setToDMY​(java.lang.String dateString) throws DateTimeFormatException
      Sets this DateStamp object corresponding to the specified string representation.

      The string has to have the following specific format:

      dd/MM/yyyy, e.g., 11/04/1976

      Parameters:
      dateString - the string representation of the date stamp (in the format dd/MM/yyyy)
      Throws:
      DateTimeFormatException - if an error occurred during conversion
      See Also:
      SimpleDateFormat
    • setToYMD

      public void setToYMD​(java.lang.String dateString) throws DateTimeFormatException
      Sets this DateStamp object corresponding to another string representation.

      The string has to have the following specific format:

      yyyy-MM-dd, e.g., 1976-04-11

      Parameters:
      dateString - the string representation of the date stamp (in the format yyyy-MM-dd)
      Throws:
      DateTimeFormatException - if an error occurred during conversion
      See Also:
      SimpleDateFormat
    • setToNextDay

      public void setToNextDay​(int nrOfNextDays)
      Sets this DateStamp to the next day.
      Parameters:
      nrOfNextDays - the number of days in the future
    • setToPreviousDay

      public void setToPreviousDay​(int nrOfPreviousDays)
      Sets this DateStamp to the previous day.
      Parameters:
      nrOfPreviousDays - the number of days in the past
    • setToNextMonth

      public void setToNextMonth​(int nrOfNextMonths)
      Sets this DateStamp to the next month.
      Parameters:
      nrOfNextMonths - the number of months in the future
    • setToPreviousMonth

      public void setToPreviousMonth​(int nrOfPreviousMonths)
      Sets this DateStamp to the previous month.
      Parameters:
      nrOfPreviousMonths - the number of months in the past
    • setToNextYear

      public void setToNextYear​(int nrOfNextYears)
      Sets this DateStamp to the next year.
      Parameters:
      nrOfNextYears - the number of years in the future
    • setToPreviousYear

      public void setToPreviousYear​(int nrOfPreviousYears)
      Sets this DateStamp to the previous year.
      Parameters:
      nrOfPreviousYears - the number of years in the past
    • setToNow

      public void setToNow()
      Sets this DateStamp to the current system date.
    • convertFromUnixTime

      public void convertFromUnixTime​(long unixMilliseconds, java.time.ZoneId timeZoneID)
      Converts this DateStamp 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 DateStamp 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​(java.time.ZoneId timeZoneID)
      Converts this DateStamp object to a Unix time (number of milliseconds since the epoch) using a specified time zone. The Unix time typically looks like, e.g., 1563319245000L.
      Parameters:
      timeZoneID - the ID of the time zone
      Returns:
      the Unix time (number of milliseconds since the epoch)
    • convertToUnixTime

      public long convertToUnixTime()
      Converts this DateStamp object to 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.
      Returns:
      the Unix time (number of milliseconds since the epoch)
    • getDayOfWeek

      public int getDayOfWeek()
      Returns this DateStamp's day-of-week [1-7].
      Returns:
      the day of the week corresponding to this DateStamp object
    • getDayOfMonth

      public int getDayOfMonth()
      Returns this DateStamp's day-of-month [1-31].
      Returns:
      the day of the month corresponding to this DateStamp object
    • getNumberOfDaysInMonth

      public int getNumberOfDaysInMonth()
      Returns the number of days in this DateStamp's month.
      Returns:
      the number of days in this DateStamp's month
    • getNumberOfDaysInMonth

      public static int getNumberOfDaysInMonth​(int month, int year)
      Returns the number of days in a specified month [1-12] and a given year.
      Parameters:
      month - the specified month [1-12]
      year - the year for the month
      Returns:
      the number of days in the specified month
    • getDayOfYear

      public int getDayOfYear()
      Returns this DateStamp's day-of-year.
      Returns:
      the day of year corresponding to this DateStamp object
    • getWeekOfYear

      public int getWeekOfYear()
      Returns this DateStamp's week-of-year [1-52].
      Returns:
      the week of the year corresponding to this DateStamp object
    • getMonth

      public int getMonth()
      Returns this DateStamp's month [1-12].
      Returns:
      the month
    • getYear

      public int getYear()
      Returns this DateStamp's year.
      Returns:
      the year
    • isLeapYear

      public boolean isLeapYear()
      Checks whether or not this DateStamp's year is a leap year (assuming a Gregorian calendar).
      Returns:
      true if this DateStamp's year is leap year, false otherwise
    • isWeekend

      public boolean isWeekend()
      Returns whether or not the current DateStamp is in a weekend.
      Returns:
      true if the current DateStamp is in a weekend, false otherwise
    • getDMYString

      public java.lang.String getDMYString()
      Returns a String representation of this DateStamp object in the format dd/MM/yyyy.
      Returns:
      a String representation of this DateStamp object in the format dd/MM/yyyy
      See Also:
      SimpleDateFormat
    • getYMDString

      public java.lang.String getYMDString()
      Returns a String representation of this DateStamp object in the format yyyy-MM-dd.
      Returns:
      a String representation of this DateStamp object in the format yyyy-MM-dd
      See Also:
      SimpleDateFormat
    • getFullDateString

      public java.lang.String getFullDateString()
      Returns a String representation of this DateStamp object as a fully formatted string.

      The string will have the following specific format:

      day-of-week dd month yyyy, e.g., Sunday 11 April 1976

      Note that a valid I18NL10N database must be available!

      Returns:
      a String representation of this DateStamp object as a fully formatted string
      See Also:
      I18NL10N
    • compareTo

      public int compareTo​(DateStamp otherDateStamp)
      The overloaded Comparable interface.
      Specified by:
      compareTo in interface java.lang.Comparable<DateStamp>
    • toString

      public java.lang.String toString()
      Default overloaded toString() method.
      Overrides:
      toString in class java.lang.Object
      Returns:
      a String representation of this DateStamp object in the format dd/MM/yyyy
      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 date stamp, false otherwise
    • hashCode

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

      public static java.time.ZoneId getZoneID​(java.lang.String timeZoneShortDescription)
      Helper method to convert a time zone's short description into a ZoneId object.
      Parameters:
      timeZoneShortDescription - the short description of the time zone
      Returns:
      the ZoneID object corresponding to the specified time zone's description
    • getDayOfWeekString

      public static java.lang.String getDayOfWeekString​(int dayOfWeek)
      Helper method to convert a day-of-week index [1 (Monday) - 7 (Sunday)] to a weekday name.

      Note that a valid I18NL10N database must be available!

      Parameters:
      dayOfWeek - the day-of-week index to convert [1-7]
      Returns:
      a string corresponding to the weekday name
      See Also:
      I18NL10N
    • getMonthString

      public static java.lang.String getMonthString​(int month)
      Helper method to convert a month index [1-12] to a month name.

      Note that a valid I18NL10N database must be available!

      Parameters:
      month - the month index to convert
      Returns:
      a string corresponding to the month name
      See Also:
      I18NL10N
    • getMonth

      public static int getMonth​(java.lang.String month)
      Helper method to convert a month name to a month index [1-12].

      Note that a valid I18NL10N database must be available!

      Parameters:
      month - the month name to convert
      Returns:
      a number corresponding to the month index
      See Also:
      I18NL10N