Class DateHelper


  • public class DateHelper
    extends Object
    Helper class for dates
    • Constructor Detail

      • DateHelper

        public DateHelper()
    • Method Detail

      • add

        public static Date add​(Date date,
                               ITimeInterval interval,
                               TimeZone timeZone)
        Adds the given interval to a date. If the interval (or either it's amount or field) is null, returns the date as is.
        Note that the result may be inaccurate for times - see add(Date, TimeField, int).
      • add

        public static Date add​(Date date,
                               TimeField field,
                               int amount,
                               TimeZone timeZone)
        Adds the given amount of time to the given date.

        Note that this method is NOT accurate. It simply adds the timefield via Calendar. For example, in case of a Daylight savings time shift, if the original date is in summertime and the end date in winter time, the one hour time shift is NOT applied if you add a certain number of days.
        For exact calculations, use addDecimal(java.util.Date, org.cyclos.model.utils.TimeField, double).

      • addDecimal

        public static Date addDecimal​(Date date,
                                      TimeField field,
                                      double amount)
        As add(Date, TimeField, int), but accepts a double as amount param. Is also more precise, as this method does handle daylight saving time shifts correctly.
      • convertField

        public static double convertField​(TimeField toField,
                                          int amount,
                                          TimeField fromField)
        Converts the given time interval into another time field
      • createPeriod

        public static Pair<Date,​Date> createPeriod​(PredefinedPeriodData input,
                                                         TimeZone timeZone)
        Creates a period from the parameters. For example: createPeriod(2012, PeriodType.Quarter, 1) creates a period for the first quarter of 2012.
        Parameters:
        input - a PredefinedPeriodData which is a wrapper around the needed input parameters. The following must be set on this object:
        • year the year in which the period is.
        • type the type of the period according to PeriodType.
        • ordinal the ordinal number of that periodType in the year. The following cases for PeriodType apply:
          • YEAR: the ordinal number is ignored, and a period for the complete year will be created.
          • QUARTER: the ordinal numbers 1 to 4 will create a period for the quarters 1, 2, 3 or 4 for the given year
          • MONTH: analogous, but with months. Ranges from 0 to 11, so 0 corresponds with January
          • WEEK: the ordinal number ranges from 1 to 53, and it returns a period around this weeknumber in that year. The weeknumbers are defined according to the local Java.util.Calendar-rules concerning Java.util.Calendar.WEEK_OF_YEAR
          Numbers outside the above given ranges result in null return values.
        timeZone - the timeZone of the client.
        Returns:
        null if the input is null, or year or type are null. Otherwise, a Pair of java.util.Dates is returned, where the first element is the begin, and the second the end of the period.
      • createPeriodRange

        public static List<PredefinedPeriodData> createPeriodRange​(PredefinedPeriodData input,
                                                                   int number)
        creates a list of periods as a range. The first element in the list is the period created with createPeriod(PredefinedPeriodData, TimeZone) followed by number consecutive elements. Example: createPeriodRange(input, 3) with input having year = 2012, type = PeriodType.MONTH and ordinal = 0, creates a list with three periods, being january, february and march 2012.
        Parameters:
        input -
        number - the number of elements created. A negative number creates a range going back in time; a positive number creates a range going forth in time.
        Returns:
        a List with PredefinedPeriodData.
        See Also:
        createPeriod(PredefinedPeriodData, TimeZone)
      • daysBetween

        public static double daysBetween​(Date date1,
                                         Date date2)
        Returns the number of days between 2 dates. Will be negative if date1 > date2, 0 if both are the same day or positive otherwise
      • earliest

        public static Date earliest​(Date... dates)
        a null proof method returning the earliest of any number of Dates
        Parameters:
        dates - any null arguments are ignored. If all arguments are null just returns null.
        Returns:
        the earliest date of the arguments.
      • formatDateTime

        public static String formatDateTime​(DateTime dateTime,
                                            String format)
        Formats the given date time on the given format
      • formatDuration

        public static String formatDuration​(Long time)
        Formats a duration (expressed in milliseconds) in a human-readable format. If less than a second, returns 0ms. If less than a minute, 0s. If less than an hour, returns 0:00. If larger, 0:00:00. For nulls, returns an empty string.
      • getCalendarField

        public static int getCalendarField​(TimeField field)
        Given a TimeField, return the matching constant in the Calendar class
      • getFormattedGMT

        public static String getFormattedGMT​(int offset)
        Returns the GMT formatted string for the given time zone offset
      • getFormattedGMT

        public static String getFormattedGMT​(TimeZone timezone,
                                             Date date)
        Returns the GMT formatted string for the given time zone and date.
      • getLastCompletedPeriod

        public static PredefinedPeriodData getLastCompletedPeriod​(PeriodType periodType,
                                                                  TimeZone timeZone)
        gets the last completed period as a PredefinedPeriodData object, also with the year specified. For example: getLastCompletePeriod(PeriodType.QUARTER) gives 2012 quarter II when the present time is in the third quarter of 2012
      • getLastMonthsRange

        public static PredefinedPeriodData getLastMonthsRange​(int months,
                                                              TimeZone timeZone)
        Gets a month range as a PredefinedPeriodData object
      • getOffset

        public static int getOffset​(Date time,
                                    TimeZone timeZone)
        Returns the time zone offset for the given time zone, at the given date, in relation to the server default time zone
      • getPresentMonth

        public static PredefinedPeriodData getPresentMonth​(TimeZone timeZone)
        gets the present year as a PredefinedPeriodData object
      • getPresentYear

        public static PredefinedPeriodData getPresentYear​(TimeZone timeZone)
        gets the present year as a PredefinedPeriodData object
      • getThisWeek

        public static PredefinedPeriodData getThisWeek​(TimeZone timeZone)
        gets the present week as a PredefinedPeriodData object
      • includes

        public static boolean includes​(Date begin,
                                       Date end,
                                       Date date,
                                       boolean beginAndEndNullMeansInfinite)
        Returns whether a period from begin to end includes the given date. If beginAndEndNullMeansInfinite is true, the result is always true. Otherwise, if both begin and end are null, will return false. In either case, if begin is null and end is not null, checks only the end. The same for begin.
      • isLessThanOneDayGap

        public static boolean isLessThanOneDayGap​(Date date1,
                                                  Date date2)
        Returns true if the difference between the two dates is less than 24 hours.
      • latest

        public static Date latest​(Date... dates)
        a null proof method returning the latest of any number of Dates
        Parameters:
        dates - any null arguments are ignored. If all arguments are null just returns null.
        Returns:
        the latest date of the arguments.
      • now

        public static Date now()
        Returns a Date representing the current time
      • periodBeginningAt

        public static DatePeriodDTO periodBeginningAt​(DateTime date)
        Creates a new period beginning at the given date
      • periodEndingAt

        public static DatePeriodDTO periodEndingAt​(DateTime date)
        Creates a new period ending at the given date
      • periodIncluding

        public static Pair<Date,​Date> periodIncluding​(ITimeInterval interval,
                                                            TimeZone timeZone,
                                                            Date date)
        Returns a full period with length according to the given interval, which includes the given date. For example, if the interval is 2 months, and the date is 2014-01-17, the return will be 2014-01-01 to 2014-02-28. All dates are expected to be on the server default time zone
      • previousPeriod

        public static Pair<Date,​Date> previousPeriod​(ITimeInterval interval,
                                                           TimeZone timeZone,
                                                           Date date)
        Returns the previous full period that does not include the given date (i.e instant). Example: interval: 2 months, timeZone= UYU (GMT-3), date = 2014-02-15
        returns a period of 2 months from 2013-12-01T00:00:00 to 2014-01-31T23:59:59.
      • sameDay

        public static boolean sameDay​(TimeZone clientTZ,
                                      Date date1,
                                      Date date2)
        returns true if on the same day, false if otherwise.
      • secondsSince

        public static double secondsSince​(long startTime)
        Returns the number of seconds elapsed since the given time
      • shiftToBegin

        public static Date shiftToBegin​(Date serverDate,
                                        TimeZone clientTZ)
        Shifts the given date to the day begin, according to the given client time zone (assumes the server time zone if null). The date is assumed to represent the server time zone. For example: If the server is +3h from the client, and the given date is 2000-01-01 01:30:00, it will shifted to 1999-12-31 22:30:00 (per time zone offset), as that's time point for the client, and represents a day ago. Then, it will be shifted to 1999-12-31 00:00:00, representing the beginning of that day for the client. Finally, the date will be shifted again to the server offset, and the result will be 1999-12-31 03:00:00 (using the server time zone). Handles null, returning null.
      • shiftToBegin

        public static Date shiftToBegin​(Date serverDate,
                                        TimeZone clientTZ,
                                        boolean dstOffset)
        Same as shiftToBegin(Date serverDate, TimeZone clientTZ) but if dstOffset it is true it adds the clientTZ dst offset to the result.
        Parameters:
        serverDate -
        clientTZ -
        dstOffset -
        Returns:
      • shiftToEnd

        public static Date shiftToEnd​(Date serverDate,
                                      TimeZone clientTZ)
        Shifts the given date to the day end, according to the given client time zone (assumes the server time zone if null). The date is assumed to represent the server time zone. For example: If the server is +3h from the client, and the given date is 2000-01-01 12:30:00, it will shifted to 2000-01-01 09:30:00 (per time zone offset), as that's time point for the client. Then, it will be shifted to 2000-01-01 23:59:59, representing the end of that day for the client. Finally, the date will be shifted again to the server offset, and the result will be 2000-01-02 02:59:59 (using the server time zone). Handles null, returning null.
      • shiftToEnd

        public static Date shiftToEnd​(Date serverDate,
                                      TimeZone clientTZ,
                                      boolean dstOffset)
        Same as shiftToEnd(Date, TimeZone) but if dstOffset it is true then it subtracts the clientTZ dst offset from the result.
      • subtract

        public static Date subtract​(Date date,
                                    ITimeInterval interval,
                                    TimeZone timeZone)
        Subtracts the given interval from a date. If the interval (or either it's amount or field) is null, returns the date as is.

        Note that the result may be inaccurate for times - see add(Date, TimeField, int).

      • subtract

        public static Date subtract​(Date date,
                                    TimeField field,
                                    int amount)
      • toCalendarField

        public static int toCalendarField​(TimeField field)