Class DateStamp
- All Implemented Interfaces:
java.lang.Comparable<DateStamp>
public final class DateStamp extends java.lang.Object implements java.lang.Comparable<DateStamp>
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 aDateStamp
object corresponding to the current system date.DateStamp(int weekOfYear, int year)
Constructs aDateStamp
object corresponding to the specified values.DateStamp(int day, int month, int year)
Constructs aDateStamp
object corresponding to the specified values.DateStamp(long unixMilliseconds)
Constructs aDateStamp
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 aDateStamp
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 aDateStamp
object corresponding to the specified string representation.DateStamp(DateStamp dateStamp)
Constructs aDateStamp
object as a copy of anotherDateStamp
object. -
Method Summary
Modifier and Type Method Description int
compareTo(DateStamp otherDateStamp)
The overloadedComparable
interface.void
convertFromUnixTime(long unixMilliseconds)
Converts thisDateStamp
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 thisDateStamp
object from a Unix time (number of milliseconds since the epoch) using a specified time zone.long
convertToUnixTime()
Converts thisDateStamp
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 thisDateStamp
object to a Unix time (number of milliseconds since the epoch) using a specified time zone.boolean
equals(java.lang.Object object)
Defaultequals()
operator.int
getDayOfMonth()
Returns thisDateStamp
's day-of-month [1-31].int
getDayOfWeek()
Returns thisDateStamp
'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 thisDateStamp
's day-of-year.java.lang.String
getDMYString()
Returns aString
representation of thisDateStamp
object in the format dd/MM/yyyy.java.lang.String
getFullDateString()
Returns aString
representation of thisDateStamp
object as a fully formatted string.int
getMonth()
Returns thisDateStamp
'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 thisDateStamp
'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 thisDateStamp
's week-of-year [1-52].int
getYear()
Returns thisDateStamp
's year.java.lang.String
getYMDString()
Returns aString
representation of thisDateStamp
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 aZoneId
object.int
hashCode()
boolean
isLeapYear()
Checks whether or not thisDateStamp
's year is a leap year (assuming a Gregorian calendar).boolean
isWeekend()
Returns whether or not the currentDateStamp
is in a weekend.void
set(DateStamp dateStamp)
Sets thisDateStamp
equal to another one.void
setToDMY(int day, int month, int year)
Sets thisDateStamp
to the specified values.void
setToDMY(java.lang.String dateString)
Sets thisDateStamp
object corresponding to the specified string representation.void
setToNextDay(int nrOfNextDays)
Sets thisDateStamp
to the next day.void
setToNextMonth(int nrOfNextMonths)
Sets thisDateStamp
to the next month.void
setToNextYear(int nrOfNextYears)
Sets thisDateStamp
to the next year.void
setToNow()
Sets thisDateStamp
to the current system date.void
setToPreviousDay(int nrOfPreviousDays)
Sets thisDateStamp
to the previous day.void
setToPreviousMonth(int nrOfPreviousMonths)
Sets thisDateStamp
to the previous month.void
setToPreviousYear(int nrOfPreviousYears)
Sets thisDateStamp
to the previous year.void
setToWoYY(int weekOfYear, int year)
Sets thisDateStamp
to the specified values.void
setToYMD(java.lang.String dateString)
Sets thisDateStamp
object corresponding to another string representation.java.lang.String
toString()
Default overloadedtoString()
method.Methods inherited from class java.lang.Object
clone, getClass, notify, notifyAll, wait, wait, wait
-
Constructor Details
-
DateStamp
public DateStamp()Constructs aDateStamp
object corresponding to the current system date. -
DateStamp
public DateStamp(int day, int month, int year)Constructs aDateStamp
object corresponding to the specified values.- Parameters:
day
- the day componentmonth
- the month componentyear
- the year component
-
DateStamp
public DateStamp(int weekOfYear, int year)Constructs aDateStamp
object corresponding to the specified values.- Parameters:
weekOfYear
- the week of the year componentyear
- the year component
-
DateStamp
public DateStamp(java.lang.String dateString, boolean... ymdFlag)Constructs aDateStamp
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 optionalboolean
indicating if the format is to be interpreted as yyyy-MM-dd
-
DateStamp
public DateStamp(long unixMilliseconds)Constructs aDateStamp
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 aDateStamp
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
Constructs aDateStamp
object as a copy of anotherDateStamp
object.This is the copy constructor.
- Parameters:
dateStamp
- theDateStamp
object to deep copy
-
-
Method Details
-
setToDMY
public void setToDMY(int day, int month, int year)Sets thisDateStamp
to the specified values.- Parameters:
day
- the day componentmonth
- the month componentyear
- the year component
-
setToWoYY
public void setToWoYY(int weekOfYear, int year)Sets thisDateStamp
to the specified values.- Parameters:
weekOfYear
- the week of the year componentyear
- the year component
-
set
Sets thisDateStamp
equal to another one.- Parameters:
dateStamp
- theDateStamp
to copy
-
setToDMY
Sets thisDateStamp
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
Sets thisDateStamp
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 thisDateStamp
to the next day.- Parameters:
nrOfNextDays
- the number of days in the future
-
setToPreviousDay
public void setToPreviousDay(int nrOfPreviousDays)Sets thisDateStamp
to the previous day.- Parameters:
nrOfPreviousDays
- the number of days in the past
-
setToNextMonth
public void setToNextMonth(int nrOfNextMonths)Sets thisDateStamp
to the next month.- Parameters:
nrOfNextMonths
- the number of months in the future
-
setToPreviousMonth
public void setToPreviousMonth(int nrOfPreviousMonths)Sets thisDateStamp
to the previous month.- Parameters:
nrOfPreviousMonths
- the number of months in the past
-
setToNextYear
public void setToNextYear(int nrOfNextYears)Sets thisDateStamp
to the next year.- Parameters:
nrOfNextYears
- the number of years in the future
-
setToPreviousYear
public void setToPreviousYear(int nrOfPreviousYears)Sets thisDateStamp
to the previous year.- Parameters:
nrOfPreviousYears
- the number of years in the past
-
setToNow
public void setToNow()Sets thisDateStamp
to the current system date. -
convertFromUnixTime
public void convertFromUnixTime(long unixMilliseconds, java.time.ZoneId timeZoneID)Converts thisDateStamp
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 thisDateStamp
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 thisDateStamp
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 thisDateStamp
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 thisDateStamp
's day-of-week [1-7].- Returns:
- the day of the week corresponding to this
DateStamp
object
-
getDayOfMonth
public int getDayOfMonth()Returns thisDateStamp
'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 thisDateStamp
'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 thisDateStamp
's day-of-year.- Returns:
- the day of year corresponding to this
DateStamp
object
-
getWeekOfYear
public int getWeekOfYear()Returns thisDateStamp
's week-of-year [1-52].- Returns:
- the week of the year corresponding to this
DateStamp
object
-
getMonth
public int getMonth()Returns thisDateStamp
's month [1-12].- Returns:
- the month
-
getYear
public int getYear()Returns thisDateStamp
's year.- Returns:
- the year
-
isLeapYear
public boolean isLeapYear()Checks whether or not thisDateStamp
's year is a leap year (assuming a Gregorian calendar).- Returns:
true
if thisDateStamp
's year is leap year,false
otherwise
-
isWeekend
public boolean isWeekend()Returns whether or not the currentDateStamp
is in a weekend.- Returns:
true
if the currentDateStamp
is in a weekend,false
otherwise
-
getDMYString
public java.lang.String getDMYString()Returns aString
representation of thisDateStamp
object in the format dd/MM/yyyy.- Returns:
- a
String
representation of thisDateStamp
object in the format dd/MM/yyyy - See Also:
SimpleDateFormat
-
getYMDString
public java.lang.String getYMDString()Returns aString
representation of thisDateStamp
object in the format yyyy-MM-dd.- Returns:
- a
String
representation of thisDateStamp
object in the format yyyy-MM-dd - See Also:
SimpleDateFormat
-
getFullDateString
public java.lang.String getFullDateString()Returns aString
representation of thisDateStamp
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 thisDateStamp
object as a fully formatted string - See Also:
I18NL10N
-
compareTo
The overloadedComparable
interface.- Specified by:
compareTo
in interfacejava.lang.Comparable<DateStamp>
-
toString
public java.lang.String toString()Default overloadedtoString()
method.- Overrides:
toString
in classjava.lang.Object
- Returns:
- a
String
representation of thisDateStamp
object in the format dd/MM/yyyy - See Also:
SimpleDateFormat
-
equals
public boolean equals(java.lang.Object object)Defaultequals()
operator.- Overrides:
equals
in classjava.lang.Object
- Returns:
true
if both objects denote the same date stamp,false
otherwise
-
hashCode
public int hashCode()- Overrides:
hashCode
in classjava.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 aZoneId
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
-