Package adams.core

Class DateUtils


  • public class DateUtils
    extends Object
    A helper class for common Date-related operations.
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    • Constructor Detail

      • DateUtils

        public DateUtils()
    • Method Detail

      • setTimeZone

        public static void setTimeZone​(TimeZone value)
        Sets the time zone to use globally.
        Parameters:
        value - the time zone
      • getTimeZone

        public static TimeZone getTimeZone()
        Returns the time zone in use.
        Returns:
        the time zone
      • setLocale

        public static void setLocale​(Locale value)
        Sets the locale to use globally.
        Parameters:
        value - the locale
      • getLocale

        public static Locale getLocale()
        Returns the locale in use.
        Returns:
        the locale
      • getCalendar

        public static Calendar getCalendar()
        Returns a new GregorianCalendar object with the system-wide defined locale and time zone.
        Returns:
        the calendar
      • getTimestampFormatter

        public static DateFormat getTimestampFormatter()
        Returns a date formatting object, initialized with timestamp format.
        Returns:
        the formatter
        See Also:
        Constants.TIMESTAMP_FORMAT
      • getTimestampFormatterMsecs

        public static DateFormat getTimestampFormatterMsecs()
        Returns a date formatting object, initialized with timestamp format that sports milliseconds.
        Returns:
        the formatter
        See Also:
        Constants.TIMESTAMP_FORMAT_MSECS
      • getTimeFormatter

        public static DateFormat getTimeFormatter()
        Returns a time formatting object, initialized with time format.
        Returns:
        the formatter
        See Also:
        Constants.TIME_FORMAT
      • getTimeFormatterMsecs

        public static DateFormat getTimeFormatterMsecs()
        Returns a time formatting object, initialized with time format (incl msecs).
        Returns:
        the formatter
        See Also:
        Constants.TIME_FORMAT_MSECS
      • getDateFormatter

        public static DateFormat getDateFormatter()
        Returns a date formatting object, initialized with date format.
        Returns:
        the formatter
        See Also:
        Constants.DATE_FORMAT
      • isValid

        public static boolean isValid​(String pattern)
        Checks whether the supplied parse pattern is valid.
        Parameters:
        pattern - the pattern to check
        Returns:
        true if valid
      • earlier

        public static Date earlier​(Date date,
                                   int type,
                                   int decrement)
        Returns a date earlier relative to the provided one, e.g., earlier by 2 hours: earlier(date, Calendar.HOUR, 2).
        Parameters:
        date - the date to use as basis
        type - the type of decrement, e.g., Calendar.HOUR
        decrement - the amount to decrement the date with
        Returns:
        the new date
        See Also:
        Calendar
      • later

        public static Date later​(Date date,
                                 int type,
                                 int increment)
        Returns a date later relative to the provided one, e.g., later by 2 hours: later(date, Calendar.HOUR, 2).
        Parameters:
        date - the date to use as basis
        type - the type of increment, e.g., Calendar.HOUR
        increment - the amount to increment the date with
        Returns:
        the new date
        See Also:
        Calendar
      • set

        public static Date set​(Date date,
                               int type,
                               int value)
        Returns a date with a value changed, e.g., the hours set to a specific value.
        Parameters:
        date - the date to use as basis
        type - the type of value to change, e.g., Calendar.HOUR
        value - the value to set
        Returns:
        the new date, null in case of an error
        See Also:
        Calendar
      • isBefore

        public static boolean isBefore​(Date base,
                                       Date check)
        Returns whether the "check" date is before the "base" date.
        Parameters:
        base - the basis of the comparison
        check - the date to compare with
        Returns:
        true if "check" is before "base"
      • isAfter

        public static boolean isAfter​(Date base,
                                      Date check)
        Returns whether the "check" date is after the "base" date.
        Parameters:
        base - the basis of the comparison
        check - the date to compare with
        Returns:
        true if "check" is after "base"
      • difference

        public static Long difference​(Date older,
                                      Date newer)
        Returns the difference in time.
        Parameters:
        older - the older timestamp
        newer - the new timestamp
        Returns:
        the time difference in msec, null if "older < newer" does not hold
      • differenceDays

        public static Double differenceDays​(Date older,
                                            Date newer)
        Returns the difference in time in days.
        Parameters:
        older - the older timestamp
        newer - the new timestamp
        Returns:
        the time difference in days, null if "older < newer" does not hold
      • differenceHours

        public static Double differenceHours​(Date older,
                                             Date newer)
        Returns the difference in time in hours.
        Parameters:
        older - the older timestamp
        newer - the new timestamp
        Returns:
        the time difference in hours, null if "older < newer" does not hold
      • differenceMinutes

        public static Double differenceMinutes​(Date older,
                                               Date newer)
        Returns the difference in time in minutes.
        Parameters:
        older - the older timestamp
        newer - the new timestamp
        Returns:
        the time difference in minutes, null if "older < newer" does not hold
      • differenceSeconds

        public static Double differenceSeconds​(Date older,
                                               Date newer)
        Returns the difference in time in seconds.
        Parameters:
        older - the older timestamp
        newer - the new timestamp
        Returns:
        the time difference in seconds, null if "older < newer" does not hold
      • msecToString

        public static String msecToString​(Long msec)
        Turns the milli-seconds into a string: hh:mm:ss.sss
        Parameters:
        msec - the milli-seconds to convert
        Returns:
        the string representation
      • parseString

        public static Date parseString​(String s,
                                       String format)
        Parses the given date/time string according to the format.
        Parameters:
        s - the date/time string to parse
        format - the format to use for parsing
        Returns:
        the parsed date or null in case of an error
      • parseString

        public static Date parseString​(String s,
                                       DateFormatString format)
        Parses the given date/time string according to the format.
        Parameters:
        s - the date/time string to parse
        format - the format to use for parsing
        Returns:
        the parsed date or null in case of an error
      • now

        public static DateTime now()
        Returns a new DateTime object with the current date/time.
        Returns:
        the current date/time
      • nowTime

        public static Time nowTime()
        Returns a new Time object with the current time.
        Returns:
        the current time
      • today

        public static Date today()
        Returns a Date object of today, at the start of the day (00:00:00.000).
        Returns:
        the current date
      • tomorrow

        public static Date tomorrow()
        Returns a Date object of tomorrow, at the start of the day (00:00:00.000).
        Returns:
        the date
      • yesterday

        public static Date yesterday()
        Returns a Date object of yesterday, at the start of the day (00:00:00.000).
        Returns:
        the date
      • absoluteDay

        protected static int absoluteDay​(Calendar cal,
                                         boolean use1904windowing)
        Given a Calendar, return the number of days since 1900/12/31.
        Parameters:
        cal - the Calendar
        Returns:
        days number of days since 1900/12/31
        Throws:
        IllegalArgumentException - if date is invalid
      • daysInPriorYears

        protected static int daysInPriorYears​(int yr,
                                              boolean use1904windowing)
        Return the number of days in prior years since 1900
        Parameters:
        yr - a year (1900 < yr < 4000)
        use1904windowing -
        Returns:
        days number of days in years prior to yr.
        Throws:
        IllegalArgumentException - if year is outside of range.
      • internalGetExcelDate

        protected static double internalGetExcelDate​(Calendar date,
                                                     boolean use1904windowing)
      • msecToSerialDate

        public static double msecToSerialDate​(long msec)
        Converts the milli-seconds (Java Epoch) to a Excel serial date (days since 0-jan-1900).
        Parameters:
        msec - the java epoch in msec
        Returns:
        the serial date
      • msecToSerialDate

        public static double msecToSerialDate​(Date date)
        Converts the date to a Excel serial date (days since 0-jan-1900).
        Parameters:
        date - the date
        Returns:
        the serial date
      • isValidExcelDate

        protected static boolean isValidExcelDate​(double value)
        Given a double, checks if it is a valid Excel date.
        Parameters:
        value - the double value
        Returns:
        true if valid
      • setCalendar

        protected static void setCalendar​(Calendar calendar,
                                          int wholeDays,
                                          int millisecondsInDay,
                                          boolean use1904windowing,
                                          boolean roundSeconds)
      • getJavaCalendar

        protected static Calendar getJavaCalendar​(double date,
                                                  boolean use1904windowing,
                                                  TimeZone timeZone,
                                                  boolean roundSeconds)
        Get EXCEL date as Java Calendar with given time zone.
        Parameters:
        date - The Excel date.
        use1904windowing - true if date uses 1904 windowing, or false if using 1900 date windowing.
        timeZone - The TimeZone to evaluate the date in
        roundSeconds - round to closest second
        Returns:
        Java representation of the date, or null if date is not a valid Excel date
      • serialDateToDate

        public static Date serialDateToDate​(double serial)
        Converts the Excel serial date (days since 0-jan-1900) to a Java date.
        Parameters:
        serial - the serial date
        Returns:
        the date
      • serialDateToMsec

        public static long serialDateToMsec​(double serial)
        Converts the Excel serial date (days since 0-jan-1900) to milli-seconds (Java Epoch).
        Parameters:
        serial - the serial date
        Returns:
        the java epoch in msec
      • checkTime

        public static boolean checkTime​(String s)
        Checks whether the string represents a time.
        Parameters:
        s - the string to check
        Returns:
        true if time
      • checkTime

        public static boolean checkTime​(String s,
                                        DateFormat df)
        Checks whether the string represents a time.
        Parameters:
        s - the string to check
        df - the format, can be null
        Returns:
        true if time
      • checkTimeMsec

        public static boolean checkTimeMsec​(String s)
        Checks whether the string represents a time with msecs.
        Parameters:
        s - the string to check
        Returns:
        true if time with msecs
      • checkTimeMsec

        public static boolean checkTimeMsec​(String s,
                                            DateFormat df)
        Checks whether the string represents a time with msecs.
        Parameters:
        s - the string to check
        df - the format, can be null
        Returns:
        true if time with msecs
      • checkDate

        public static boolean checkDate​(String s)
        Checks whether the string represents a date.
        Parameters:
        s - the string to check
        Returns:
        true if date
      • checkDate

        public static boolean checkDate​(String s,
                                        DateFormat df)
        Checks whether the string represents a date.
        Parameters:
        s - the string to check
        df - the format, can be null
        Returns:
        true if date
      • checkDateTime

        public static boolean checkDateTime​(String s)
        Checks whether the string represents a date/time.
        Parameters:
        s - the string to check
        Returns:
        true if date/time
      • checkDateTime

        public static boolean checkDateTime​(String s,
                                            DateFormat df)
        Checks whether the string represents a date/time.
        Parameters:
        s - the string to check
        df - the format, can be null
        Returns:
        true if date/time
      • checkDateTimeMsec

        public static boolean checkDateTimeMsec​(String s)
        Checks whether the string represents a date/time msec.
        Parameters:
        s - the string to check
        Returns:
        true if date/time msec
      • checkDateTimeMsec

        public static boolean checkDateTimeMsec​(String s,
                                                DateFormat df)
        Checks whether the string represents a date/time msec.
        Parameters:
        s - the string to check
        df - the format, can be null
        Returns:
        true if date/time msec