psdi.app.common

Class DateUtility

  • java.lang.Object
    • psdi.app.common.DateUtility


  • public class DateUtility
    extends java.lang.Object
    A bunch of utility functions used for time calculation purposes
    • Constructor Summary

      Constructors 
      Constructor and Description
      DateUtility() 
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method and Description
      static java.util.Date addDays(java.util.Date date, int plusDays)
      Add a number of days to a date.
      static java.util.Date addHours(java.util.Date date, double hours)
      Add a number of hours to a date.
      static java.util.Date addMinutes(java.util.Date date, int plusMinutes)
      Add a number of minutes to a date.
      static java.util.Date addMonths(java.util.Date date, int plusMonths)
      Add a number of months to a date.
      static java.util.Date addSeconds(java.util.Date date, int plusSeconds)
      Add a number of seconds to a date.
      static boolean after(java.util.Date clickdate, java.util.Date start)
      Returns true if clickdate is after start date.
      static boolean afterOrEquals(java.util.Date clickdate, java.util.Date start)
      Returns true if clickdate is equals or after start date.
      static boolean before(java.util.Date clickdate, java.util.Date end)
      Returns true if the clickdate is before the end date.
      static boolean beforeOrEquals(java.util.Date clickdate, java.util.Date end)
      Returns true if the clickdate is equals or before the end date.
      static boolean between(java.util.Date clickdate, Range range, boolean startInclusive, boolean endInclusive)
      Returns true if the clickdate is inside the given range.
      static boolean betweenDaysOnly(java.util.Date clickdate, Range range, boolean startInclusive, boolean endInclusive) 
      static java.util.Date combineDate(java.util.Date date1, java.util.Date date2)
      Make a new Date from the date part of date1 and time part of date2
      static int compareTime(java.util.Date date1, java.util.Date date2)
      See if date1 if before date2 in hours/minutes only.
      static java.util.Date convertDateToUserTimezone(java.util.Date d, java.util.Locale userLocale, java.util.TimeZone userTimeZone)
      Returns the date in user timezone and locale
      static double convertToMinutes(double time, java.lang.String uom)
      convert a time to number of minutes based on the unit of measure.
      The uom can be one of these,
      MINUTES HOURS DAYS
      static double durationBetweenTwoDates(java.util.Date startTime, java.util.Date endTime)
      Calculate the duration between two dates.
      static java.lang.String formatDate(java.util.Date date)
      Formats a given date that according to the U.S.
      static java.util.Date getDate(java.util.Date date)
      Get the date with hour/minute/second/millisecond stripped
      static java.util.Date getDate(java.util.Date date, java.util.TimeZone currentTimeZone, java.util.TimeZone newTimeZone)
      Converts a date from one Time Zone to another.
      static java.util.Date getDateHourMinute(java.util.Date date)
      Trims a Date to the start of the day
      static java.util.Date getEndOfDayDate(java.util.Date date)
      Get the date with hour/minute/second/millisecond maxed out
      static java.util.Date getFirstDayMonth(java.util.Date date)
      Returns first day of the month.
      static java.util.Date getFirstDayWeek(java.util.Date date)
      Returns first day of the week.
      static java.util.Date getLastDayMonth(java.util.Date date)
      Returns last day of the month.
      static java.util.Date getLastDayWeek(java.util.Date date)
      Returns first day of the week.
      static java.util.Date getMaxDate(java.util.Date d1, java.util.Date d2)
      Returns the maximum of two dates.
      static java.util.Date getMinDate(java.util.Date d1, java.util.Date d2)
      Returns the minimum of two dates.
      static int getMonth(java.util.Date date) 
      static java.util.Date getNextDayAfter(java.util.Date date, int day)
      Get next
      static int getOffset(int currentOffset, int newOffset) 
      static java.util.Set mergeDates(java.util.Collection set) 
      static java.util.Set mergeDates(java.util.Collection set1, java.util.Collection set2) 
      static java.util.Date moveDate(java.util.Date date, java.util.TimeZone currentTimeZone, java.util.TimeZone newTimeZone)
      Converts a date from one Time Zone to another.
      static int safeCompareTo(java.util.Date d1, java.util.Date d2)
      Performs a "compare" on two dates, accounting for null.
      static boolean safeEquals(java.util.Date d1, java.util.Date d2)
      Performs and "equals" on two dates, accounting for null.
      static int timeDiff(java.util.Date date1, java.util.Date date2)
      Time difference(in minutes) between date1 and date2 in hours/minutes.
      static int timeDiffInDays(java.util.Date date1, java.util.Date date2)
      Time difference(in days) between date1 and date2 in days.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DateUtility

        public DateUtility()
    • Method Detail

      • mergeDates

        public static java.util.Set mergeDates(java.util.Collection set1,
                                               java.util.Collection set2)
      • mergeDates

        public static java.util.Set mergeDates(java.util.Collection set)
      • convertToMinutes

        public static double convertToMinutes(double time,
                                              java.lang.String uom)
        convert a time to number of minutes based on the unit of measure.
        The uom can be one of these,
        MINUTES HOURS DAYS
      • getDate

        public static java.util.Date getDate(java.util.Date date)
        Get the date with hour/minute/second/millisecond stripped
      • getDateHourMinute

        public static java.util.Date getDateHourMinute(java.util.Date date)
        Trims a Date to the start of the day
        Parameters:
        in - Date
        Returns:
        Date trimmed to the start of the day
      • getEndOfDayDate

        public static java.util.Date getEndOfDayDate(java.util.Date date)
        Get the date with hour/minute/second/millisecond maxed out
      • addDays

        public static java.util.Date addDays(java.util.Date date,
                                             int plusDays)
        Add a number of days to a date. return the new date
      • addHours

        public static java.util.Date addHours(java.util.Date date,
                                              double hours)
        Add a number of hours to a date. return the new date
      • addMinutes

        public static java.util.Date addMinutes(java.util.Date date,
                                                int plusMinutes)
        Add a number of minutes to a date. return the new date
      • addSeconds

        public static java.util.Date addSeconds(java.util.Date date,
                                                int plusSeconds)
        Add a number of seconds to a date. return the new date
      • compareTime

        public static int compareTime(java.util.Date date1,
                                      java.util.Date date2)
        See if date1 if before date2 in hours/minutes only. The other parts of the dates are ignored.
      • timeDiff

        public static int timeDiff(java.util.Date date1,
                                   java.util.Date date2)
        Time difference(in minutes) between date1 and date2 in hours/minutes. The other parts of the dates are ignored
      • timeDiffInDays

        public static int timeDiffInDays(java.util.Date date1,
                                         java.util.Date date2)
        Time difference(in days) between date1 and date2 in days. The other parts of the dates are ignored
      • combineDate

        public static java.util.Date combineDate(java.util.Date date1,
                                                 java.util.Date date2)
        Make a new Date from the date part of date1 and time part of date2
      • getMaxDate

        public static java.util.Date getMaxDate(java.util.Date d1,
                                                java.util.Date d2)
        Returns the maximum of two dates. A null date is treated as being less than any non-null date.
      • getMinDate

        public static java.util.Date getMinDate(java.util.Date d1,
                                                java.util.Date d2)
        Returns the minimum of two dates. A null date is treated as being greater than any non-null date.
      • convertDateToUserTimezone

        public static java.util.Date convertDateToUserTimezone(java.util.Date d,
                                                               java.util.Locale userLocale,
                                                               java.util.TimeZone userTimeZone)
        Returns the date in user timezone and locale
        Parameters:
        l -
        tz -
        Returns:
        Date
        Throws:
        java.rmi.RemoteException
      • getDate

        public static java.util.Date getDate(java.util.Date date,
                                             java.util.TimeZone currentTimeZone,
                                             java.util.TimeZone newTimeZone)
        Converts a date from one Time Zone to another.
        Parameters:
        date -
        currentTimeZone -
        newTimeZone -
        Returns:
      • moveDate

        public static java.util.Date moveDate(java.util.Date date,
                                              java.util.TimeZone currentTimeZone,
                                              java.util.TimeZone newTimeZone)
        Converts a date from one Time Zone to another. NOTE: This is used in Scheduler, please do not modify or remove this method, there were several side effects in Scheduler when the getDate(Date, TimeZone, TimeZone) method was changed, and we had to create this separate method to fix them.
        Parameters:
        date -
        currentTimeZone -
        newTimeZone -
        Returns:
      • getOffset

        public static int getOffset(int currentOffset,
                                    int newOffset)
      • formatDate

        public static java.lang.String formatDate(java.util.Date date)
        Formats a given date that according to the U.S. standards.
        Parameters:
        date -
        Returns:
      • durationBetweenTwoDates

        public static double durationBetweenTwoDates(java.util.Date startTime,
                                                     java.util.Date endTime)
        Calculate the duration between two dates.
        Parameters:
        startDate -
        endDate -
        Returns:
        duration
        Throws:
        java.lang.Exception
      • safeEquals

        public static boolean safeEquals(java.util.Date d1,
                                         java.util.Date d2)
        Performs and "equals" on two dates, accounting for null. This differs from a normal Date equals because if you compare a TimeStamp (which is a Date) and a Date and they have the same time, they will not be equal. The method compares their getTime() values and ensures that if a Timestamp is being compared to a Date and they have the same time, then, they will also be equal.
        Parameters:
        d1 -
        d2 -
        Returns:
      • safeCompareTo

        public static int safeCompareTo(java.util.Date d1,
                                        java.util.Date d2)
        Performs a "compare" on two dates, accounting for null. This differs from a normal Date compareTo because if you compare a TimeStamp (which is a Date) and a Date and they have the same time, they will not be equal. The method compares their getTime() values and ensures that if a Timestamp is being compared to a Date and they have the same time, then, they will also be equal.
        Parameters:
        d1 -
        d2 -
        Returns:
      • after

        public static boolean after(java.util.Date clickdate,
                                    java.util.Date start)
        Returns true if clickdate is after start date. This differs from Date.before() in that this only compares to the second, and not the millisecond
        Parameters:
        clickdate -
        start -
        Returns:
      • before

        public static boolean before(java.util.Date clickdate,
                                     java.util.Date end)
        Returns true if the clickdate is before the end date. This differs from Date.before() in that this only compares to the second, and not the millisecond
        Parameters:
        clickdate -
        end -
        Returns:
      • afterOrEquals

        public static boolean afterOrEquals(java.util.Date clickdate,
                                            java.util.Date start)
        Returns true if clickdate is equals or after start date. Uses the same logic as DateUtility.after
        Parameters:
        clickdate -
        start -
        Returns:
      • beforeOrEquals

        public static boolean beforeOrEquals(java.util.Date clickdate,
                                             java.util.Date end)
        Returns true if the clickdate is equals or before the end date. Uses the same logic as DateUtility.before Only compares to the second, and not the millisecond
        Parameters:
        clickdate -
        end -
        Returns:
      • between

        public static boolean between(java.util.Date clickdate,
                                      Range range,
                                      boolean startInclusive,
                                      boolean endInclusive)
        Returns true if the clickdate is inside the given range. if startInclusive is true, clickdate is considered inside range when equals to the start date. if endInclusive is true, clickdate is considered inside range when equals to the end date.
        Parameters:
        clickdate -
        range -
        startInclusive -
        endInclusive -
        Returns:
      • betweenDaysOnly

        public static boolean betweenDaysOnly(java.util.Date clickdate,
                                              Range range,
                                              boolean startInclusive,
                                              boolean endInclusive)
      • getNextDayAfter

        public static java.util.Date getNextDayAfter(java.util.Date date,
                                                     int day)
        Get next
        Parameters:
        date - - Current Date
        day - - Day of the week (Calendar.MONDAY, Calendar.TUESDAY, etc.)
        Returns:
      • getFirstDayWeek

        public static java.util.Date getFirstDayWeek(java.util.Date date)
        Returns first day of the week.
        Parameters:
        date -
        Returns:
      • getLastDayWeek

        public static java.util.Date getLastDayWeek(java.util.Date date)
        Returns first day of the week.
        Parameters:
        date -
        Returns:
      • getFirstDayMonth

        public static java.util.Date getFirstDayMonth(java.util.Date date)
        Returns first day of the month.
        Parameters:
        date -
        Returns:
      • getLastDayMonth

        public static java.util.Date getLastDayMonth(java.util.Date date)
        Returns last day of the month.
        Parameters:
        date -
        Returns:
      • addMonths

        public static java.util.Date addMonths(java.util.Date date,
                                               int plusMonths)
        Add a number of months to a date. return the new date
      • getMonth

        public static int getMonth(java.util.Date date)