Class DateUtils

java.lang.Object
overit.geocallapp.utilities.datatype.DateUtils

public class DateUtils extends Object
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 DateUtils with additional utilities for business applications.
Since:
1.0
  • Field Details

    • CODING_TIMEMILLISEC

      public static final String CODING_TIMEMILLISEC
      Default time format with seconds and milliseconds: "HH:mm:ss.SSS".
      See Also:
    • CODING_DAYMONTHTIME

      public static final String CODING_DAYMONTHTIME
      Default date and time format without year: "dd/MM HH:mm".
      See Also:
  • Method Details

    • getTime2359

      public static Date getTime2359()
      Returns a Date representing the time 23:59 (11:59 PM).
      Returns:
      a Date with time set to 23:59 (11:59 PM)
    • getHourSecondsSimpleDateFormat

      public static SimpleDateFormat getHourSecondsSimpleDateFormat()
      Returns the time in format: "HH:mm:ss".
      Returns:
      the time
    • getValueOfDate

      public static int getValueOfDate(int field, Date date)
      Returns the value (int) of the Calendar field for a date. Keep in mind that the months are between 0 - 11. Ex: getValOFDate(Calendar.YEAR, new Date()) returns the current year.
      Parameters:
      field - the Calendar field constant to extract (e.g., Calendar.YEAR, Calendar.MONTH)
      date - the Date from which to extract the field value
      Returns:
      the integer value of the specified calendar field
    • getDate

      public static Date getDate(int day, int month, int year, int hour, int min, int sec)
      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 Date with the specified date and time components
    • mergeTimeWithSecondsToDate

      public static Date mergeTimeWithSecondsToDate(Date day, Date hour)
      Returns the date merging the given date and the given hour.
      Parameters:
      day - the Date containing the date component to use
      hour - the Date containing the time component to extract and merge
      Returns:
      the Date with the date from the first parameter and time from the second parameter
    • max

      public static Date max(Date date1, Date date2)
      Return the maximum date between the two given.
      Parameters:
      date1 - the first Date to compare (can be null)
      date2 - the second Date to compare (can be null)
      Returns:
      the maximum (later) of the two dates, or the non-null date if one is null, or null if both are null
    • min

      public static Date min(Date date1, Date date2)
      Return the minimum date between the two given.
      Parameters:
      date1 - the first Date to compare (can be null)
      date2 - the second Date to compare (can be null)
      Returns:
      the earlier of the two dates, or the non-null date if one is null, or null if both are null
    • getTimeWithSecondsFromDate

      public static Date getTimeWithSecondsFromDate(Date date)
      Returns the date in format: "HH:mm:ss".
      Parameters:
      date - the Date from which to extract the time component
      Returns:
      a new Date containing only the time portion in "HH:mm:ss" format, or null if parsing fails
    • truncateMilliseconds

      public static Date truncateMilliseconds(Date date)
      Truncates the milliseconds in the given date.
      Parameters:
      date - the Date to truncate (must not be null)
      Returns:
      a new Date with the millisecond component set to zero
    • isToday

      public static boolean isToday(Date date)
      Checks whether the given date is today.
      Parameters:
      date - the Date to check against today's date
      Returns:
      true if the date falls on the current day (ignoring time component)
    • isInTheFuture

      public static boolean isInTheFuture(Date date)
      Checks whether the given date is in the future.
      Parameters:
      date - the Date to check against today's date
      Returns:
      true if the date is after the current day (ignoring time component)
    • isBetween

      public static boolean isBetween(Date date, Date from, Date to)
      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 - the Date to check if it falls within the specified range
      from - the start Date of the range (can be null for open-ended range)
      to - the end Date of the range (can be null for open-ended range)
      Returns:
      true if the date falls between the from and to dates (exclusive boundaries)
    • isBetween

      public static boolean isBetween(Date date, Date from, Date to, boolean withBoundaries)
      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 - the Date to check if it falls within the specified range
      from - the start Date of the range (can be null for open-ended range)
      to - the end Date of 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

      public static Date getFirstDayOfMonth(Date d)
      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

      public static Date getFirstDayOfYear(Date d)
      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

      public static Date getFirstDayOfYear(int year)
      Returns the first day of the given year.
      Parameters:
      year - the given year
      Returns:
      the first day of the year
    • getLastDayOfMonth

      public static Date getLastDayOfMonth(Date d)
      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

      public static Date getLastDayOfYear(Date d)
      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

      public static Date getLastDayOfYear(int year)
      Returns the last day of the given year.
      Parameters:
      year - the given year
      Returns:
      the last day of the year
    • getFirstDayOfWeek

      public static Date getFirstDayOfWeek(Date d)
      Returns the first day of the week in
      Parameters:
      d - the given date
      Returns:
      the first day of the week
    • getCalendar

      public static Calendar getCalendar(Locale locale)
      Returns the Calendar's date from the given Locale's one
      Parameters:
      locale - the Locale that the Calendar must use
      Returns:
      a Calendar instance
    • sumWeekToDate

      public static Date sumWeekToDate(Date date, int value)
      Sums the given week to the given date.
      Parameters:
      date - the Date to which weeks will be added
      value - the number of weeks to add (can be negative to subtract)
      Returns:
      a new Date with the specified number of weeks added
    • sumSecondToDate

      public static Date sumSecondToDate(Date date, int value)
      Sums the given seconds to the given date.
      Parameters:
      date - the Date to which seconds will be added
      value - the number of seconds to add (can be negative to subtract)
      Returns:
      a new Date with the specified number of seconds added
    • sameDay

      public static boolean sameDay(Date a, Date b)
      Checks whether the two dates fall in the same day.
      Parameters:
      a - the first Date to compare (can be null)
      b - the second Date to compare (can be null)
      Returns:
      true if both dates fall on the same calendar day (ignoring time), false if either is null
    • sameWeek

      public static boolean sameWeek(Date a, Date b)
      Checks whether the two dates fall in the same week.
      Parameters:
      a - the first Date to compare
      b - the second Date to compare
      Returns:
      true if both dates fall within the same calendar week
    • sameMonth

      public static boolean sameMonth(Date a, Date b)
      Checks whether the two dates fall in the same month.
      Parameters:
      a - the first Date to compare
      b - the second Date to compare
      Returns:
      true if both dates fall within the same calendar month and year
    • combineDateTime

      public static Date combineDateTime(Date date, Date time)
      Merges the given date with the given time.
      Parameters:
      date - the Date containing the date component to use
      time - the Date containing the time component to extract and merge
      Returns:
      a new Date combining the date from the first parameter with the time from the second parameter
    • centralDate

      public static Date 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

      public static ZonedDateTime centralZonedDateTime()
      Method introduced during the time zones support. Returns the current date and time as a ZonedDateTime in the central (server) timezone. It's used as placeholder for ZonedDateTime.now() occurrences for which we already decide to use the server locale.
      Returns:
      a new ZonedDateTime
    • centralToday

      public static Date centralToday()
      Returns the current date in the central time zone.
      Returns:
      the central time zone date
    • localToday

      public static Date localToday(TimeZone timeZone)
      Returns the current date in the local time zone.
      Parameters:
      timeZone - the time zone
      Returns:
      the current local date
    • localDate

      public static Date localDate(TimeZone timeZone)
      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 - the TimeZone to project the current instant to
      Returns:
      the current instant in the specified timezone if timezones are enabled, central date otherwise
    • loggedUserDate

      public static Date loggedUserDate()
      Return the date using the user's time zone
      Returns:
      the new Date localized to the logged user time zone
    • getYearBoudaries

      public static Date[] getYearBoudaries(Date targetDate)
      Returns an array containing the first and the last day of the year.
      Parameters:
      targetDate - a target date
      Returns:
      an array of 2 Dates for which the first Date is the first day of the year, and the second Date is the last day of the year, starting from the given target date
    • getYearBoudaries

      public 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.
      Parameters:
      targetDate - a target date
      locale - the locale to use for the dates conventions
      Returns:
      an array of 2 Dates for which the first Date is the first day of the year, and the second Date is the last day of the year, starting from the given target date
    • getMonthBoudaries

      public static Date[] getMonthBoudaries(Date targetDate)
      Returns an array containing the first and the last day of the month.
      Parameters:
      targetDate - a target date
      Returns:
      an array of 2 Dates for which the first Date is the first day of the month, and the second Date is the last day of the month, starting from the given target date
    • getMonthBoudaries

      public 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.
      Parameters:
      targetDate - a target date
      locale - the locale to use for the dates conventions
      Returns:
      an array of 2 Dates for which the first Date is the first day of the month, and the second Date is the last day of the month, starting from the given target date
    • getWeekBoudaries

      public static Date[] getWeekBoudaries(Date targetDate)
      Returns an array containing the first and the last day of the week.
      Parameters:
      targetDate - a target date
      Returns:
      an array of 2 Dates for which the first Date is the first day of the week, and the second Date is the last day of the week, starting from the given target date
    • getWeekBoudaries

      public 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.
      Parameters:
      targetDate - a target date
      locale - the locale to use for the dates conventions
      Returns:
      an array of 2 Dates for which the first Date is the first day of the week, and the second Date is the last day of the week, starting from the given target date
    • getWeekBoudariesISO8601

      public static Date[] getWeekBoudariesISO8601(Date targetDate)
      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

      public static Date[] getDayBoudaries(Date targetDate)
      Returns an array containing the first and the last date of the day.
      Parameters:
      targetDate - a target date
      Returns:
      an array of 2 Dates for which the first Date is the day begin, and the second Date is the day end, starting from the given target date
    • getDayBoudaries

      public 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.
      Parameters:
      targetDate - a target date
      locale - the locale to use for the dates conventions
      Returns:
      an array of 2 Dates for which the first Date is the day begin, and the second Date is the day end, starting from the given target date
    • getTimeFromSerial

      public static Getter getTimeFromSerial(long value)
      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 Getter containing the time breakdown, or null if value is negative
    • getTimeFromSerial

      public static Getter getTimeFromSerial(long value, int unit)
      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 a Getter object with keys corresponding to Calendar field constants.
      Parameters:
      value - the serial time value to convert
      unit - the unit of the input value (Calendar.MILLISECOND, Calendar.SECOND, etc.)
      Returns:
      a Getter containing the time breakdown with Calendar field keys, or null if value is negative
    • getYearFromDate

      public static int getYearFromDate(Date date)
      Extracts the year component from a date.
      Parameters:
      date - the Date from which to extract the year
      Returns:
      the year as an integer
    • getMonthFromDate

      public static int getMonthFromDate(Date date)
      Extracts the month component from a date. Note that months are zero-based (January = 0, February = 1, etc.).
      Parameters:
      date - the Date from which to extract the month
      Returns:
      the month as an integer (0-11, where 0 = January)
    • getWeekFromDate

      public static int getWeekFromDate(Date date)
      Extracts the week of year component from a date. The week numbering follows the calendar's locale-specific rules.
      Parameters:
      date - the Date from which to extract the week
      Returns:
      the week of year as an integer