Class DateUtils

java.lang.Object
overit.geocall.util.DateUtils

public class DateUtils extends Object
Auxiliary class to manage dates.
See Also:
  • Field Details

  • Constructor Details

    • DateUtils

      protected DateUtils()
  • Method Details

    • timeMin

      public static Date timeMin(TimeZone tz)
      Return the TIME_MIN converted to the input time zone. The time to convert is based on the time zone of the JVM.
      Parameters:
      tz - Time zone to use for the conversion.
      Returns:
      The date containing the converted time.
    • timeMax

      public static Date timeMax(TimeZone tz)
      Return the TIME_MAX converted to the input time zone. The time to convert is based on the time zone of the JVM.
      Parameters:
      tz - Time zone to use for the conversion.
      Returns:
      The date containing the converted time.
    • sdfOra

      public static final SimpleDateFormat sdfOra()
      Return the default format to apply on the time string.
      Returns:
      The SimpleDateFormat for the time.
    • sdfData

      public static final SimpleDateFormat sdfData()
      Return the default format to apply on the date string.
      Returns:
      The SimpleDateFormat for the date.
    • sdfDataOra

      public static final SimpleDateFormat sdfDataOra()
      Return the default format to apply on the date and time string.
      Returns:
      The SimpleDateFormat for the date and time.
    • sdfDataOraSecondi

      public static final SimpleDateFormat sdfDataOraSecondi()
      Return the default format to apply on the date and time string containing the seconds.
      Returns:
      The SimpleDateFormat for the date and time with seconds.
    • mergeTimeToDate

      public static Date mergeTimeToDate(Date day, Date hour)
      Create and return a new date merging the two date inputs: day and hour. The formats used are sdfData() for the day, sdfOra() for the time and sdfDataOra() for the merge.
      Parameters:
      day - The day to use in the merge.
      hour - The time to use in the merge.
      Returns:
      The new date resulting from the union of the two inputs.
    • mergeTimeToDate

      public static Date mergeTimeToDate(Date date, String sdfTime, String sTime, TimeZone tz)
      Create and return a new date merging the date passed as first parameter with the time built with the combination of the other three parameters.
      If the format of the time or the string containing the time are null, the date passed in input will be returned.
      Parameters:
      date - The day to use in the merge.
      sdfTime - The string containing the format to use to parse the time string (see SimpleDateFormat).
      sTime - The string containing the value of the time to use in the merge.
      tz - The TimeZone of the time passed as parameter.
      Returns:
      The new date resulting from the merge of the day with the time.
    • mergeDateToTime

      public static Date mergeDateToTime(Date time, String sdfDate, String sDate, TimeZone tz)
      Create and return a new date merging the time passed as first parameter with the day built with the combination of the other three parameters.
      If the format of the day or the string containing the day are null, the time passed in input will be returned.
      Parameters:
      time - The time to use in the merge.
      sdfDate - The string containing the format to use to parse the string of the day (see SimpleDateFormat).
      sDate - The string containing the value of the day to use in the merge.
      tz - The TimeZone of the day passed as parameter.
      Returns:
      The new date resulting from the merge of the time with the day.
    • getTimeFromDate

      public static Date getTimeFromDate(Date date)
      Get only the time from the input date and create a new date containing the union of this time with the day "01 January 1970". The format is determinated by sdfOra().
      Parameters:
      date - The date from which to extract the time.
      Returns:
      The date containing the union of the time extract from the input with the day "01 January 1970".
    • getDayFromDate

      public static Date getDayFromDate(Date date)
      Get only the day from the input date and create a new date containing the union of this day with the time "00:00:00". The format is determinated by sdfData().
      Parameters:
      date - The date from which to extract the day.
      Returns:
      The date containing the union of the day extract from the input with the time "00:00:00".
    • sumSecondToDate

      public static Date sumSecondToDate(Date date, int seconds)
      Add the number of seconds passed as second input to the date passed as first input and return the new date.
      Parameters:
      date - The date to add the seconds.
      seconds - Number of seconds to add to the date.
      Returns:
      The date generated by adding the seconds.
    • sumMinuteToDate

      public static Date sumMinuteToDate(Date date, int minutes)
      Add the number of minutes passed as second input to the date passed as first input and return the new date.
      Parameters:
      date - The date to add the minutes.
      minutes - Number of minutes to add to the date.
      Returns:
      The date generated by adding the minutes.
    • sumHourToDate

      public static Date sumHourToDate(Date date, int hours)
      Add the number of hours passed as second input to the date passed as first input and return the new date.
      Parameters:
      date - The date to add the hours.
      hours - Number of hours to add to the date.
      Returns:
      The date generated by adding the hours.
    • sumDayToDate

      public static Date sumDayToDate(Date date, int days)
      Add the number of days passed as second input to the date passed as first input and return the new date.
      Parameters:
      date - The date to add the days.
      days - Number of days to add to the date.
      Returns:
      The date generated by adding the days.
    • sumYearToDate

      public static Date sumYearToDate(Date date, int years)
      Add the number of years passed as second input to the date passed as first input and return the new date.
      Parameters:
      date - The date to add the years.
      years - Number of years to add to the date.
      Returns:
      The date generated by adding the years.
    • sumMonthToDate

      public static Date sumMonthToDate(Date date, int months)
      Add the number of months passed as second input to the date passed as first input and return the new date.
      Parameters:
      date - The date to add the months.
      months - Number of months to add to the date.
      Returns:
      The date generated by adding the months.
    • getDateDifference

      @Deprecated public static int getDateDifference(Date from, Date to, int unit)
      Calculate the difference between two dates, keeping in mind the daylight savings time. Return a number that rappresents the amount of time expressed with the unit of measure passed in the unit parameter.
      Parameters:
      from - Start date.
      to - End date.
      unit - Unit of measure of the difference. The accepted values are:
      Returns:
      The difference between the start date and end date.
    • getInterval

      public static int getInterval(TimeZone tz, Date from, Date to, int unit)
      Calculate the difference between two dates, keeping in mind the daylight savings time. Return a number that rappresents the amount of time expressed with the unit of measure passed in the unit parameter.
      Parameters:
      tz - TimeZone
      from - Start date.
      to - End date.
      unit - Unit of measure of the difference. The accepted values are:
      Returns:
      The difference between the start date and end date.
    • getLocalInterval

      public static int getLocalInterval(Date from, Date to, int unit)
      Calculate the difference between two dates, keeping in mind the daylight savings time. Return a number that rappresents the amount of time expressed with the unit of measure passed in the unit parameter.
      Parameters:
      from - Start date.
      to - End date.
      unit - Unit of measure of the difference. The accepted values are:
      Returns:
      The difference between the start date and end date.
    • getCombinedDateTime

      public static Date getCombinedDateTime(Date hours, Date date) throws ParseException
      This method take an hour and a date (both in Date format) and create a combination of them
      Parameters:
      hours - The published values's HashGetter
      date - Actual InputDate's value
      Returns:
      A Date which is the combination of date and hours
      Throws:
      ParseException - If an error has been reached unexpectedly while parsing
    • getRegexpFromDateFormat

      public static String getRegexpFromDateFormat(String pattern)
      This method extracts the regular expression related to the format of the date passed in input. Date format must respect the java Date and Time Patterns
      Parameters:
      pattern - the pattern describing the date format
      Returns:
      the regexp matches the passed date pattern
    • trunc

      public static Date trunc(Date d, int unit)
      Returns a copy of the passed date truncated to the specified unit. Truncate the given date at the specified time unit. The supported units are:
      • Calendar.MILLISECOND: to round down to the nearest millisecond
      • Calendar.SECOND: to round down to the nearest second, setting the millis to 0
      • Calendar.MINUTE: to round down to the nearest minute, setting the seconds and millis to 0
      • Calendar.HOUR_OF_DAY: to round down to the nearest hour, setting the minute, seconds and millis to 0
      • Calendar.DAY_OF_WEEK: to round down to the nearest day, setting the hours, minutes, seconds and millis to 0
      • Calendar.DAY_OF_MONTH: to round down to the nearest first day of week, setting the hours, minutes, seconds and millis to 0
      • Calendar.MONTH: to round down to the nearest first month, setting the first day and the hours, minutes, seconds and millis to 0
      Parameters:
      d - the date that will be truncated
      unit - the time unit
      Returns:
      the truncated date
    • getInstantFromDate

      public static Instant getInstantFromDate(Date date)
      Convert a date into an Instant instance
      Parameters:
      date - the date to convert
      Returns:
      the instant instance or null if the passed date is null
    • getDateFromInstant

      public static Date getDateFromInstant(Instant instant)
      Convert an instant into a Date instance
      Parameters:
      instant - the instant to convert
      Returns:
      the date instance or null if the passed instant is null