Class DateStamp

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

public final class DateStamp extends Object implements 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
    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, ZoneId timeZoneID)
    Constructs a DateStamp object corresponding to the specified Unix time (number of milliseconds since the epoch) using a specified time zone.
    DateStamp(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, ZoneId timeZoneID)
    Converts this DateStamp object from a Unix time (number of milliseconds since the epoch) using a specified time zone.
    long
    Converts this DateStamp object to a Unix time (number of milliseconds since the epoch) using the user's local time zone.
    long
    Converts this DateStamp object to a Unix time (number of milliseconds since the epoch) using a specified time zone.
    boolean
    equals(Object object)
    Default equals() operator.
    int
    Returns this DateStamp's day-of-month [1-31].
    int
    Returns this DateStamp's day-of-week [1-7].
    static String
    getDayOfWeekString(int dayOfWeek)
    Helper method to convert a day-of-week index [1 (Monday) - 7 (Sunday)] to a weekday name.
    int
    Returns this DateStamp's day-of-year.
    Returns a String representation of this DateStamp object in the format dd/MM/yyyy.
    Returns a String representation of this DateStamp object as a fully formatted string.
    int
    Returns this DateStamp's month [1-12].
    static int
    Helper method to convert a month name to a month index [1-12].
    static String
    getMonthString(int month)
    Helper method to convert a month index [1-12] to a month name.
    int
    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
    Returns this DateStamp's week-of-year [1-52].
    int
    Returns this DateStamp's year.
    Returns a String representation of this DateStamp object in the format yyyy-MM-dd.
    static ZoneId
    getZoneID(String timeZoneShortDescription)
    Helper method to convert a time zone's short description into a ZoneId object.
    int
     
    boolean
    Checks whether or not this DateStamp's year is a leap year (assuming a Gregorian calendar).
    boolean
    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(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
    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(String dateString)
    Sets this DateStamp object corresponding to another string representation.
    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(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, 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(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:
    • setToYMD

      public void setToYMD(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:
    • 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, 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(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 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:
    • getYMDString

      public 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:
    • getFullDateString

      public 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:
    • compareTo

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

      public String toString()
      Default overloaded toString() method.
      Overrides:
      toString in class Object
      Returns:
      a String representation of this DateStamp object in the format dd/MM/yyyy
      See Also:
    • equals

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

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

      public static ZoneId getZoneID(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 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:
    • getMonthString

      public static 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:
    • getMonth

      public static int getMonth(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: