Class DateUtils
java.lang.Object
overit.geocallapp.utilities.datatype.DateUtils
Utility class providing comprehensive date and time manipulation operations.
This class offers static methods for date calculations, timezone handling, boundary calculations, and various date formatting operations. It extends the functionality of the core
This class offers static methods for date calculations, timezone handling, boundary calculations, and various date formatting operations. It extends the functionality of the core
DateUtils
with additional utilities for business applications. - Since:
- 1.0
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic DateMethod introduced during the time zones support.static DateReturns the current date in the central time zone.static ZonedDateTimeMethod introduced during the time zones support.static DatecombineDateTime(Date date, Date time) Merges the given date with the given time.static CalendargetCalendar(Locale locale) static DategetDate(int day, int month, int year, int hour, int min, int sec) Starting from the given parameters, it returns the corresponding date.static Date[]getDayBoudaries(Date targetDate) Returns an array containing the first and the last date of the day.static Date[]getDayBoudaries(Date targetDate, Locale locale) Returns an array containing the first and the last date of the day, using the specified locale for the dates conventions.static DateReturns the first day of the month in which the given date falls.static DateReturns the first day of the week instatic DategetFirstDayOfYear(int year) Returns the first day of the given year.static DateReturns the first day of the year in which the given date falls.static SimpleDateFormatReturns the time in format: "HH:mm:ss".static DateReturns the last day of the month in which the given date falls.static DategetLastDayOfYear(int year) Returns the last day of the given year.static DateReturns the last day of the year in which the given date falls.static Date[]getMonthBoudaries(Date targetDate) Returns an array containing the first and the last day of the month.static Date[]getMonthBoudaries(Date targetDate, Locale locale) Returns an array containing the first and the last day of the month, using the specified locale for the dates conventions.static intgetMonthFromDate(Date date) Extracts the month component from a date.static DateReturns aDaterepresenting the time 23:59 (11:59 PM).static GettergetTimeFromSerial(long value) Converts a serial time value to a time breakdown using seconds as the default unit.static GettergetTimeFromSerial(long value, int unit) Converts a serial time value to a detailed time breakdown.static DateReturns the date in format: "HH:mm:ss".static intgetValueOfDate(int field, Date date) Returns the value (int) of theCalendarfield for a date.static Date[]getWeekBoudaries(Date targetDate) Returns an array containing the first and the last day of the week.static Date[]getWeekBoudaries(Date targetDate, Locale locale) Returns an array containing the first and the last day of the week, using the specified locale for the dates conventions.static Date[]getWeekBoudariesISO8601(Date targetDate) Returns an array containing the first and the last day of the week using ISO-8601 week rules.static intgetWeekFromDate(Date date) Extracts the week of year component from a date.static Date[]getYearBoudaries(Date targetDate) Returns an array containing the first and the last day of the year.static Date[]getYearBoudaries(Date targetDate, Locale locale) Returns an array containing the first and the last day of the year, using the specified locale for the dates conventions.static intgetYearFromDate(Date date) Extracts the year component from a date.static booleanChecks whether the given date is between the two given dates.static booleanChecks whether the given date is between the two given dates.static booleanisInTheFuture(Date date) Checks whether the given date is in the future.static booleanChecks whether the given date is today.static DateReturns the current date in the specified time zone, if time zone support is active.static DatelocalToday(TimeZone timeZone) Returns the current date in the local time zone.static DateReturn the date using the user's time zonestatic DateReturn the maximum date between the two given.static DatemergeTimeWithSecondsToDate(Date day, Date hour) Returns the date merging the given date and the given hour.static DateReturn the minimum date between the two given.static booleanChecks whether the two dates fall in the same day.static booleanChecks whether the two dates fall in the same month.static booleanChecks whether the two dates fall in the same week.static DatesumSecondToDate(Date date, int value) Sums the given seconds to the given date.static DatesumWeekToDate(Date date, int value) Sums the given week to the given date.static DatetruncateMilliseconds(Date date) Truncates the milliseconds in the given date.
-
Field Details
-
CODING_TIMEMILLISEC
Default time format with seconds and milliseconds:"HH:mm:ss.SSS".- See Also:
-
CODING_DAYMONTHTIME
Default date and time format without year:"dd/MM HH:mm".- See Also:
-
-
Method Details
-
getTime2359
Returns aDaterepresenting the time 23:59 (11:59 PM).- Returns:
- a
Datewith time set to 23:59 (11:59 PM)
-
getHourSecondsSimpleDateFormat
Returns the time in format: "HH:mm:ss".- Returns:
- the time
-
getValueOfDate
Returns the value (int) of theCalendarfield for a date. Keep in mind that the months are between 0 - 11. Ex: getValOFDate(Calendar.YEAR, new Date()) returns the current year. -
getDate
Starting from the given parameters, it returns the corresponding date.- Parameters:
day- the day of the month (1-31)month- the month (1-12, where 1 = January)year- the year (e.g., 2023)hour- the hour of day (0-23)min- the minutes (0-59)sec- the seconds (0-59)- Returns:
- the constructed
Datewith the specified date and time components
-
mergeTimeWithSecondsToDate
Returns the date merging the given date and the given hour. -
max
Return the maximum date between the two given. -
min
Return the minimum date between the two given. -
getTimeWithSecondsFromDate
Returns the date in format: "HH:mm:ss". -
truncateMilliseconds
Truncates the milliseconds in the given date. -
isToday
Checks whether the given date is today.- Parameters:
date- theDateto check against today's date- Returns:
- true if the date falls on the current day (ignoring time component)
-
isInTheFuture
Checks whether the given date is in the future.- Parameters:
date- theDateto check against today's date- Returns:
- true if the date is after the current day (ignoring time component)
-
isBetween
Checks whether the given date is between the two given dates. If the 'from' parameter is null, it checks only if the date is earlier than the 'to' parameter. If the 'to' parameter is null, it checks only if the date is later than the 'from' parameter. -
isBetween
Checks whether the given date is between the two given dates. If the 'from' parameter is null, it checks only if the date is earlier than the 'to' parameter. If the 'to' parameter is null, it checks only if the date is later than the 'from' parameter.- Parameters:
date- theDateto check if it falls within the specified rangefrom- the startDateof the range (can be null for open-ended range)to- the endDateof the range (can be null for open-ended range)withBoundaries- true to include boundary dates in the range (inclusive), false for exclusive boundaries- Returns:
- true if the date falls between the from and to dates according to the boundary setting
-
getFirstDayOfMonth
Returns the first day of the month in which the given date falls.- Parameters:
d- the given date- Returns:
- the first day of the month
-
getFirstDayOfYear
Returns the first day of the year in which the given date falls.- Parameters:
d- the given date- Returns:
- the first day of the year
-
getFirstDayOfYear
Returns the first day of the given year.- Parameters:
year- the given year- Returns:
- the first day of the year
-
getLastDayOfMonth
Returns the last day of the month in which the given date falls.- Parameters:
d- the given date- Returns:
- the last day of the month
-
getLastDayOfYear
Returns the last day of the year in which the given date falls.- Parameters:
d- the given date- Returns:
- the last day of the year
-
getLastDayOfYear
Returns the last day of the given year.- Parameters:
year- the given year- Returns:
- the last day of the year
-
getFirstDayOfWeek
Returns the first day of the week in- Parameters:
d- the given date- Returns:
- the first day of the week
-
getCalendar
-
sumWeekToDate
Sums the given week to the given date. -
sumSecondToDate
Sums the given seconds to the given date. -
sameDay
Checks whether the two dates fall in the same day. -
sameWeek
Checks whether the two dates fall in the same week. -
sameMonth
Checks whether the two dates fall in the same month. -
combineDateTime
Merges the given date with the given time. -
centralDate
Method introduced during the time zones support. It's used as placeholder for the new Date() occurrences for which we already decide to use the server locale.- Returns:
- a new
Date
-
centralZonedDateTime
Method introduced during the time zones support. Returns the current date and time as aZonedDateTimein the central (server) timezone. It's used as placeholder forZonedDateTime.now()occurrences for which we already decide to use the server locale.- Returns:
- a new
ZonedDateTime
-
centralToday
Returns the current date in the central time zone.- Returns:
- the central time zone date
-
localToday
Returns the current date in the local time zone.- Parameters:
timeZone- the time zone- Returns:
- the current local date
-
localDate
Returns the current date in the specified time zone, if time zone support is active. If enabled, it returns the current instant projected to the specified timezone. If disabled, it returns the central server date.- Parameters:
timeZone- theTimeZoneto project the current instant to- Returns:
- the current instant in the specified timezone if timezones are enabled, central date otherwise
-
loggedUserDate
Return the date using the user's time zone- Returns:
- the new
Datelocalized to the logged user time zone
-
getYearBoudaries
Returns an array containing the first and the last day of the year. -
getYearBoudaries
Returns an array containing the first and the last day of the year, using the specified locale for the dates conventions. -
getMonthBoudaries
Returns an array containing the first and the last day of the month. -
getMonthBoudaries
Returns an array containing the first and the last day of the month, using the specified locale for the dates conventions. -
getWeekBoudaries
Returns an array containing the first and the last day of the week. -
getWeekBoudaries
Returns an array containing the first and the last day of the week, using the specified locale for the dates conventions. -
getWeekBoudariesISO8601
Returns an array containing the first and the last day of the week using ISO-8601 week rules.- Parameters:
targetDate- the target date- Returns:
- an array of
Date, where the first element is the first day of the week and the second element is the last day of the week (following ISO-8601 week rules)
-
getDayBoudaries
Returns an array containing the first and the last date of the day. -
getDayBoudaries
Returns an array containing the first and the last date of the day, using the specified locale for the dates conventions. -
getTimeFromSerial
Converts a serial time value to a time breakdown using seconds as the default unit.- Parameters:
value- the serial time value to convert- Returns:
- a
Gettercontaining the time breakdown, or null if value is negative
-
getTimeFromSerial
Converts a serial time value to a detailed time breakdown. This method breaks down a time value into its constituent parts (days, hours, minutes, seconds, milliseconds) and returns them in aGetterobject with keys corresponding toCalendarfield constants.- Parameters:
value- the serial time value to convertunit- the unit of the input value (Calendar.MILLISECOND, Calendar.SECOND, etc.)- Returns:
- a
Gettercontaining the time breakdown with Calendar field keys, or null if value is negative
-
getYearFromDate
Extracts the year component from a date.- Parameters:
date- theDatefrom which to extract the year- Returns:
- the year as an integer
-
getMonthFromDate
Extracts the month component from a date. Note that months are zero-based (January = 0, February = 1, etc.).- Parameters:
date- theDatefrom which to extract the month- Returns:
- the month as an integer (0-11, where 0 = January)
-
getWeekFromDate
Extracts the week of year component from a date. The week numbering follows the calendar's locale-specific rules.- Parameters:
date- theDatefrom which to extract the week- Returns:
- the week of year as an integer
-