Package com.storedobject.common
Class DateUtility
java.lang.Object
com.storedobject.common.DateUtility
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedA utility class for performing date-related operations. -
Method Summary
Modifier and TypeMethodDescriptionstatic <D extends Date>
DaddDay(D date, int day) Adds a particular number of days to a specified datestatic <D extends Date>
DaddMonth(D date, int month) Adds a particular number of months to a specified datestatic <D extends Date>
DaddYear(D date, int year) Adds a particular number of years to a specified datestatic <D extends Date>
Dclone(D date) Duplicates the date object passedstatic <D extends Date>
intcompareTo(D one, D two) Compare two datesstatic Datecreate()Create and return the Date instance for the current datestatic Datecreate(int dateValue) Get a date corresponding to a particular date value in a yearstatic Datecreate(int year, int month, int day) Create a Date instance when day, month, year are specified.create(D date) Get a java.sql.Date corresponding to a given java.util.Datestatic DateGet a date by passing date string separated by spaces, commas, hyphens, or slashes.static DateGet the java.sql.Date from a java.time.LocalDatestatic DateCreate a Date instance by passing a Calendar instancestatic TimecreateTime(LocalDateTime date) Get time by passing LocalDateTimestatic TimecreateTime(Calendar calendar) Get the Time value corresponding to a Calendar instancestatic TimecreateTime(Date date) Get the Time value corresponding to a java.util.Date instancestatic TimestampcreateTimestamp(long time) Creates aTimestampinstance from the specified epoch time.static TimestampcreateTimestamp(LocalDateTime date) Get TimeStamp by passing LocalDateTimestatic TimestampcreateTimestamp(Date date) static DateFormatReturns a clone of the Java DateFormat instance.static DateFormatReturns a clone of the default date and time format.difference(D from, int days) Get the difference between two dates as a string representation in the form "3Y 2M 4D" (meaning 3 years, 2 months, 4 days).difference(D one, D two) Get the difference between two dates as a string representation in the form "3Y 2M 4D" (meaning 3 years, 2 months, 4 days).static DateGet end date of current month.static DateendOfMonth(int monthOffset) Get an end date of a month.static <D extends Date>
DendOfMonth(int monthOffset, D date) Get an end date of a month.static <D extends Date>
DendOfMonth(D date) Get end date of month for a give datestatic TimestampGets the timestamp for the current date with the time set to end time of the daystatic DateGet end date of current yearstatic <D extends Date>
DendOfYear(D date) Get end date of year for a give datestatic TimestampGets the timestamp for the previous date with the time set to end time of the daystatic TimestampGets the timestamp for the date provided and the time set to end time of that daystatic <D extends Date>
booleanequals(D one, D two) Check whether two dates are the sameformat(D date) Formats the given dateformatDate(D date) Formats a given date in SQL date formatstatic StringformatHHMM(long time) Formats the given time in milliseconds into a string in HH:mm format.formatHHMM(D date) Formats the given date into a string representing the time in HH:mm format.formatLongDate(D date) Formats a given date and timeformatMonth(D date) Formats the given date into a string representing the month.formatShortDate(D date) Formats a given date in MMM dd, yy formatformatTime(D date) Formats the given date object into a string representation according to the pre-defined time format.formatTinyDate(D date) Formats a given date in MM yy formatformatWithTime(D date) Formats the given dateformatWithTimeHHMM(D date) Formats the given datestatic <D extends Date>
DConverts the given date from GMT to the specified time zone.static <D extends Date>
DConverts the given date from GMT (Greenwich Mean Time) to a specified time zone.static <D extends Date>
DfromGMT(D dateGMT, ZoneOffset toZoneOffset) Translates a given date from GMT to a specified time zone.static <D extends Date>
DConverts a given date from GMT (Greenwich Mean Time) to the specified time zone.static <D extends Date>
intget(D date, int field) Returns the value of the specified calendar field extracted from the given date.static intgetDay()Retrieves the current day of the month based on the system's current date.static <D extends Date>
intgetDay(D date) Retrieves the day of the month from the specified date.static <D extends Date>
intgetDayOfYear(D date) Get the day of the year.static <D extends Date>
intgetHourOfYear(D date) Get the hour of the year.static intgetMonth()Retrieves the current month as an integer value.static <D extends Date>
intgetMonth(D date) Returns the month of the year for the given date.static intRetrieves the numeric value of a month based on its name.static StringgetMonthName(int m) Returns the name of the month corresponding to the given month number.getMonthName(D date) Retrieves the name of the month for the specified date.static String[]Returns an array of month names.static intRetrieves the current month as a zero-based index (0 for January, 1 for February, etc.).static <D extends Date>
intgetMonthZeroBased(D date) Retrieves the month from the given date as a zero-based value.static <D extends Date>
intgetPeriodInDays(D one, D two) Get the difference in days between two datesstatic <D extends Date>
intgetPeriodInDays(D one, D two, boolean considerTime) Get the difference in days between two dates by considering the time as wellstatic <D extends Date>
intgetPeriodInMonths(D one, D two) Get the difference in months between two datesstatic String[]Retrieves the names of the days in a week.static <D extends Date>
intgetWeekOfYear(D date) Get the week of the year.static intgetYear()Retrieves the current year based on the system's local date.static <D extends Date>
intgetYear(D date) Gets the year value for a particular datestatic booleanisLeapYear(int year) Checks if a particular year specified is leap yearstatic <D extends Date>
booleanisLeapYear(D date) Checks if a particular year is a leap year by taking in a datestatic <D extends Date>
booleanisSameDate(D one, D two) Validates and returns true if two dates passed to the method are the samelocal(D date) Get java.time.LocalDate by passing a Datestatic <D extends Date>
LocalDateTimelocalTime(D date) Get java.time.LocalDateTime by passing a Datestatic DateReturns the maximum possible date value.static Timestampnow()Get the current timestampstatic <D extends Date>
DsetDay(D date, int day) Sets the day of the given date to the specified day value.static <D extends Date>
DsetMonth(D date, int month) Sets the month of this date to the specified value.static <D extends Date>
DsetYear(D date, int year) Sets the specified year for the given date object and returns the updated date.static DateGet the start date of the current monthstatic DatestartOfMonth(int monthOffset) Get a start date of a month.static <D extends Date>
DstartOfMonth(int monthOffset, D date) Get a starting date of a month.static <D extends Date>
DstartOfMonth(D date) Get start date of month for a give datestatic TimestampGets the timestamp for the current date with the time set to start time of the daystatic TimestampGets the timestamp for the next date with the time set to start time of the daystatic DateGet the start date of the current yearstatic <D extends Date>
DstartOfYear(D date) Get the start date of year for a given datestatic TimestampGets a timestamp for the date provided and the time set to start time of that daystatic Timetime()Gets the current date and time value as aTime.static DateFormatRetrieves a clone of the time format used by the application.static DateFormatReturns a cloned instance of the default 24-hour time format.static Datetoday()Get the current datestatic <D extends Date>
DConverts a given date to Greenwich Meantime (GMT) based on the provided time zone.static <D extends Date>
DConverts a given date to the GMT timezone.static <D extends Date>
DtoGMT(D date, ZoneOffset fromZoneOffset) Converts the given date to GMT (Greenwich Mean Time) by subtracting the offset caused by the provided ZoneOffset.static <D extends Date>
DConverts the given date to GMT (Greenwich Mean Time) by converting its time zone to GMT.static Datetomorrow()Get one day after the current datestatic TimeTrims the hours portion of the given time value.static TimestampTrims the hours, minutes, seconds, and milliseconds from the given Timestamp, effectively resetting the time to midnight of the same date.static TimetrimMillis(Time time) Trims the millisecond portion of the given time value.static TimestamptrimMillis(Timestamp time) Trims the milliseconds portion of the providedTimestamp.static TimetrimMinutes(Time time) Trims the minutes portion of the given time value.static TimestamptrimMinutes(Timestamp time) Adjusts the given Timestamp by removing the minutes and seconds components, effectively rounding down to the start of the nearest hour.static TimetrimSeconds(Time time) Trims the seconds portion of the given time value.static TimestamptrimSeconds(Timestamp time) Trims the seconds and milliseconds from the given Timestamp, rounding down to the nearest minute.static DateGet one day prior to the current date
-
Constructor Details
-
DateUtility
protected DateUtility()A utility class for performing date-related operations. This class is not intended to be instantiated directly. The constructor is protected to prevent unauthorized instantiation from outside the class while allowing inheritance if necessary.
-
-
Method Details
-
today
-
tomorrow
Get one day after the current date- Returns:
- One day after the current date
-
yesterday
Get one day prior to the current date- Returns:
- One day prior to the current date
-
startOfYear
Get the start date of the current year- Returns:
- Start date of the current year
-
startOfYear
Get the start date of year for a given date- Type Parameters:
D- Date-type- Parameters:
date- Date for which the start of the year is to be returned- Returns:
- Start date of year for a give date
-
endOfYear
-
endOfYear
Get end date of year for a give date- Type Parameters:
D- Date-type- Parameters:
date- Date for which the end of the year is to be returned- Returns:
- End date of year for a give date
-
startOfMonth
Get the start date of the current month- Returns:
- Start date of the current month
-
startOfMonth
Get start date of month for a give date- Type Parameters:
D- Date-type- Parameters:
date- Date for which the start of the month is to be returned- Returns:
- Start date of month for a give date
-
startOfMonth
Get a start date of a month. Month is calculated by adding an offset to the current month- Parameters:
monthOffset- An integer offset value that has to be added to the current month value- Returns:
- Start date of month
-
startOfMonth
Get a starting date of a month.- Type Parameters:
D- Date-type- Parameters:
monthOffset- An integer offset value that needs to be added to the month corresponding to a given date.date- Date for which the start of the month is to be returned.- Returns:
- Start date of month
-
endOfMonth
Get end date of current month.- Returns:
- End date of the current month
-
endOfMonth
Get end date of month for a give date- Type Parameters:
D- Date-type- Parameters:
date- Date for which the end of the month is to be returned- Returns:
- Start date of month for a give date
-
endOfMonth
Get an end date of a month. Month is calculated by adding an offset to the current month- Parameters:
monthOffset- An integer offset value that has to be added to the current month value- Returns:
- Start date of month
-
endOfMonth
Get an end date of a month. Month is calculated by adding an offset to the month corresponding to a given date- Type Parameters:
D- Date-type- Parameters:
monthOffset- An integer offset value that has to be added to the month corresponding to a given datedate- Date for which the end of the month is to be returned.- Returns:
- End date of month
-
create
-
create
Create and return the Date instance for the current date- Returns:
- Current date
-
create
Create a Date instance when day, month, year are specified.- Parameters:
year- Year.month- Month (1-based, not 0-based).day- Day.- Returns:
- Date for the specified day, month and year
-
create
-
create
Get a date corresponding to a particular date value in a year- Parameters:
dateValue- an integer representing the date- Returns:
- Date corresponding to the given date value in integer
-
create
Get a date by passing date string separated by spaces, commas, hyphens, or slashes. It could contain just digits (with or without separators) or strings such as "Aug 15, 1947", "23 Jan 1998", etc.- Parameters:
text- Date in text format- Returns:
- Date corresponding to the given date text
-
create
-
createTime
Get time by passing LocalDateTime- Parameters:
date- LocalDateTime- Returns:
- Time
-
createTimestamp
Get TimeStamp by passing LocalDateTime- Parameters:
date- LocalDateTime- Returns:
- Timestamp
-
local
-
localTime
Get java.time.LocalDateTime by passing a Date- Type Parameters:
D- Date-type- Parameters:
date- Date- Returns:
- date Local date-time
-
createTime
-
createTime
-
createTimestamp
-
createTimestamp
-
time
-
trimMillis
-
trimSeconds
-
trimMinutes
-
trimHours
-
now
-
startTime
-
endTime
-
endOfToday
Gets the timestamp for the current date with the time set to end time of the day- Returns:
- timestamp
-
endOfYesterday
Gets the timestamp for the previous date with the time set to end time of the day- Returns:
- timestamp
-
startOfToday
Gets the timestamp for the current date with the time set to start time of the day- Returns:
- timestamp
-
startOfTomorrow
Gets the timestamp for the next date with the time set to start time of the day- Returns:
- Timestamp
-
trimMillis
-
trimSeconds
-
trimMinutes
Adjusts the given Timestamp by removing the minutes and seconds components, effectively rounding down to the start of the nearest hour.- Parameters:
time- the Timestamp object to be trimmed; must not be null.- Returns:
- a new Timestamp object with minutes and seconds set to zero, or null if the input is null.
-
trimHours
Trims the hours, minutes, seconds, and milliseconds from the given Timestamp, effectively resetting the time to midnight of the same date.- Parameters:
time- the Timestamp to be trimmed; must not be null- Returns:
- a new Timestamp set to midnight of the same date as the input
-
formatMonth
Formats the given date into a string representing the month.- Type Parameters:
D- the type of the date parameter, which must extend java.util.Date- Parameters:
date- the date to be formatted; must not be null- Returns:
- a string representation of the month extracted from the provided date
-
formatDate
-
formatLongDate
-
formatShortDate
-
formatTinyDate
-
format
-
formatWithTime
-
formatWithTimeHHMM
-
formatTime
Formats the given date object into a string representation according to the pre-defined time format.- Type Parameters:
D- the type of the date parameter, which must extend java.util.Date- Parameters:
date- the date object to be formatted; must be a subclass of java.util.Date- Returns:
- a string representation of the formatted time
-
formatHHMM
Formats the given date into a string representing the time in HH:mm format.- Type Parameters:
D- the type of the date parameter, which must extend java.util.Date- Parameters:
date- the date object to format, must not be null- Returns:
- a string representing the time in HH:mm format
-
formatHHMM
Formats the given time in milliseconds into a string in HH:mm format.- Parameters:
time- the time in milliseconds to be formatted- Returns:
- a string representation of the given time formatted as HH:mm
-
equals
Check whether two dates are the same- Type Parameters:
D- Date-type- Parameters:
one- first date to comparetwo- second date to compare- Returns:
- a boolean true if one and two are the same, otherwise false
-
compareTo
Compare two dates- Type Parameters:
D- Date-type- Parameters:
one- Date to comparetwo- Date to compare- Returns:
- A negative integer, zero, or a positive integer as 'one' is less than, equal to, or greater than 'two'.
-
getMonth
Retrieves the numeric value of a month based on its name.- Parameters:
monthName- the name of the month to be converted to its numeric representation- Returns:
- the numeric value of the month (1 for January, 2 for February, etc.), or 0 if the month name is not recognized
-
getMonthName
Returns the name of the month corresponding to the given month number.- Parameters:
m- the month number (1 for January, 2 for February, ..., 12 for December)- Returns:
- the name of the month, or null if the input month number is out of range
-
getMonthName
Retrieves the name of the month for the specified date.- Type Parameters:
D- The type of the Date object, which must extendDate.- Parameters:
date- The date object from which the month name will be derived. Must not be null.- Returns:
- The full name of the month corresponding to the provided date.
-
getYear
public static int getYear()Retrieves the current year based on the system's local date.- Returns:
- the current year as an integer
-
getYear
Gets the year value for a particular date- Type Parameters:
D- Date-type- Parameters:
date- Date- Returns:
- year corresponding to the date passed
-
setYear
Sets the specified year for the given date object and returns the updated date.- Type Parameters:
D- the type of the date object, which must extendDate- Parameters:
date- the date object to modifyyear- the year to set in the date object- Returns:
- the updated date object with the specified year set
-
getWeekOfYear
Get the week of the year.- Type Parameters:
D- Date type.- Parameters:
date- Date- Returns:
- Week number.
-
getDayOfYear
Get the day of the year.- Type Parameters:
D- Date type.- Parameters:
date- Date- Returns:
- Day of the year.
-
getHourOfYear
Get the hour of the year.- Type Parameters:
D- Date type.- Parameters:
date- Date- Returns:
- Hour of the year.
-
getMonth
public static int getMonth()Retrieves the current month as an integer value.- Returns:
- the current month, where January is 1 and December is 12
-
getMonth
Returns the month of the year for the given date.- Type Parameters:
D- The type of the date parameter, which must extend java.util.Date.- Parameters:
date- The date object from which the month is to be extracted.- Returns:
- The month of the year as an integer (1 for January, 2 for February, ..., 12 for December).
-
getMonthZeroBased
public static int getMonthZeroBased()Retrieves the current month as a zero-based index (0 for January, 1 for February, etc.).- Returns:
- the zero-based index of the current month
-
getMonthZeroBased
Retrieves the month from the given date as a zero-based value.- Type Parameters:
D- the type of the date parameter, which must extend java.util.Date- Parameters:
date- the date from which the month is to be extracted; must not be null- Returns:
- the zero-based month (0 for January, 1 for February, ..., 11 for December)
-
setMonth
Sets the month of this date to the specified value.- Type Parameters:
D- Date-type- Parameters:
date- The original datemonth- the month value that has to be set- Returns:
- Date after setting the month value
-
getDay
public static int getDay()Retrieves the current day of the month based on the system's current date.- Returns:
- the day of the month as an integer
-
getDay
Retrieves the day of the month from the specified date.- Type Parameters:
D- the type of the date parameter, which must extend java.util.Date- Parameters:
date- the date object from which the day of the month is to be extracted; must not be null- Returns:
- the day of the month as an integer
-
setDay
Sets the day of the given date to the specified day value.- Type Parameters:
D- the type of the date object, which must extendjava.util.Date- Parameters:
date- the date object whose day is to be setday- the day value to set in the date object- Returns:
- the updated date object with the new day value
-
addYear
Adds a particular number of years to a specified date- Type Parameters:
D- Date-type- Parameters:
date- The date to which the years are addedyear- The number of years to add to the date passed- Returns:
- The date after adding the year offset
-
addMonth
Adds a particular number of months to a specified date- Type Parameters:
D- Date-type- Parameters:
date- The date to which the months are addedmonth- The number of months to add to the date passed- Returns:
- The date after adding the month offset
-
addDay
Adds a particular number of days to a specified date- Type Parameters:
D- Date-type- Parameters:
date- The date to which the days are addedday- The number of days to add to the date passed- Returns:
- The date after adding the days offset
-
clone
Duplicates the date object passed- Type Parameters:
D- Date-type- Parameters:
date- Date- Returns:
- A duplicate of the date passed
-
isLeapYear
public static boolean isLeapYear(int year) Checks if a particular year specified is leap year- Parameters:
year- Year- Returns:
- true if the year passed to the method is leap year
-
isLeapYear
Checks if a particular year is a leap year by taking in a date- Type Parameters:
D- Date-type- Parameters:
date- Date- Returns:
- true if the year in the date passed to the method is leap year
-
getPeriodInDays
Get the difference in days between two dates- Type Parameters:
D- Date-type- Parameters:
one- Date that has to be subtracted from the greater datetwo- The greater date from which the other date is subtracted- Returns:
- The difference between date two and date one in days
-
getPeriodInDays
Get the difference in days between two dates by considering the time as well- Type Parameters:
D- Date-type- Parameters:
one- Date that has to be subtracted from the greater datetwo- The greater date from which the other date is subtractedconsiderTime- Whether to consider the time component in the calculation- Returns:
- The difference between date two and date one in days
-
getPeriodInMonths
Get the difference in months between two dates- Type Parameters:
D- Date-type- Parameters:
one- Date that has to be subtracted from the greater datetwo- The greater date from which the other date is subtracted- Returns:
- The difference between date two and date one in months
-
get
Returns the value of the specified calendar field extracted from the given date.- Type Parameters:
D- the type of the date, which must extendDate- Parameters:
date- the date object from which the field value should be extractedfield- the calendar field to retrieve (e.g.,Calendar.YEAR,Calendar.MONTH, etc.)- Returns:
- the value of the specified calendar field for the given date
- Throws:
NullPointerException- if the date is nullIllegalArgumentException- if the specified field is not a valid calendar field
-
isSameDate
Validates and returns true if two dates passed to the method are the same- Type Parameters:
D- Date-type- Parameters:
one- First datetwo- Second date- Returns:
- True if two dates passed to the method are the same
-
difference
Get the difference between two dates as a string representation in the form "3Y 2M 4D" (meaning 3 years, 2 months, 4 days). The respective parts may be absent for zero values unless the result is just "0D".- Type Parameters:
D- Date-type- Parameters:
one- First datetwo- Second date- Returns:
- Differences between the two dates as a string.
-
difference
Get the difference between two dates as a string representation in the form "3Y 2M 4D" (meaning 3 years, 2 months, 4 days). The respective parts may be absent for zero values unless the result is just "0D".- Type Parameters:
D- Date-type- Parameters:
from- The first datedays- offset added to the date to get the second date- Returns:
- Differences between the two dates as a string.
-
maximumDate
Returns the maximum possible date value.- Returns:
- the maximum date value available
-
getMonthNames
Returns an array of month names.- Returns:
- an array of Strings representing the names of the months.
-
getWeekNames
Retrieves the names of the days in a week.- Returns:
- a string array containing the names of the days in a week
-
dateFormat
Returns a clone of the Java DateFormat instance.- Returns:
- a clone of the Java DateFormat instance.
-
dateTimeFormat
Returns a clone of the default date and time format.- Returns:
- a clone of the default date and time format.
-
timeFormat
Retrieves a clone of the time format used by the application.- Returns:
- A clone of the time format.
-
timeFormat24
Returns a cloned instance of the default 24-hour time format. The returned DateFormat object can be used to format time values in the 24-hour format.- Returns:
- a cloned instance of the default 24-hour time format
-
toGMT
Converts a given date to Greenwich Meantime (GMT) based on the provided time zone.- Type Parameters:
D- the type of the date parameter, which must extend java.util.Date- Parameters:
date- the date to be converted to GMTfromTimeZone- the time zone of the given date, represented as a string- Returns:
- the converted date in GMT
-
toGMT
Converts the given date to GMT (Greenwich Mean Time) by converting its time zone to GMT.- Type Parameters:
D- the type of the date parameter, which must extend java.util.Date- Parameters:
date- The date to be converted to GMT.fromTimeZone- The time zone of the given date. If null, the default time zone of the system will be used.- Returns:
- The converted date in GMT.
-
toGMT
-
toGMT
Converts the given date to GMT (Greenwich Mean Time) by subtracting the offset caused by the provided ZoneOffset.- Type Parameters:
D- the type of date to convert, must extend java.util.Date- Parameters:
date- the date to convert to GMTfromZoneOffset- the ZoneOffset that represents the offset from GMT- Returns:
- the converted date in GMT
-
fromGMT
Converts the given date from GMT to the specified time zone.- Type Parameters:
D- the type of date to convert, must extend java.util.Date- Parameters:
date- The date object to convert from GMT to the specified time zone. Cannot be null.toTimeZone- The target time zone to convert the date to. Can be null.- Returns:
- The converted date object in the specified time zone, or the original date object if toTimeZone is null.
-
fromGMT
Converts a given date from GMT (Greenwich Mean Time) to the specified time zone.- Type Parameters:
D- the type of date to convert, must extend java.util.Date- Parameters:
date- the date to be converted from GMTtoTimeZone- the target time zone to convert the date to, can be null- Returns:
- the converted date in the specified time zone
-
fromGMT
Converts the given date from GMT (Greenwich Mean Time) to a specified time zone.- Type Parameters:
D- the type of the date object- Parameters:
date- the date to be convertedtoZoneId- the target time zone ID to convert the date to- Returns:
- the converted date in the specified time zone
-
fromGMT
Translates a given date from GMT to a specified time zone.- Type Parameters:
D- the type of date to convert, must extend java.util.Date- Parameters:
dateGMT- the date to be translated from GMT to the specified time zone.toZoneOffset- the time zone offset to be applied to the date. If null, the date will not be modified.- Returns:
- the translated date in the specified time zone, or the original date if no offset is provided.
-