Class CachedDateFormat

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable

    public final class CachedDateFormat
    extends java.text.DateFormat
    CachedDateFormat optimizes the performance of a wrapped DateFormat. The implementation is not thread-safe. If the millisecond pattern is not recognized, the class will only use the cache if the same value is requested.
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.text.DateFormat

        java.text.DateFormat.Field
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int NO_MILLISECONDS
      Constant used to represent that there was no change observed when changing the millisecond count.
      static int UNRECOGNIZED_MILLISECONDS
      Constant used to represent that there was an observed change, but was an expected change.
      • Fields inherited from class java.text.DateFormat

        AM_PM_FIELD, calendar, DATE_FIELD, DAY_OF_WEEK_FIELD, DAY_OF_WEEK_IN_MONTH_FIELD, DAY_OF_YEAR_FIELD, DEFAULT, ERA_FIELD, FULL, HOUR_OF_DAY0_FIELD, HOUR_OF_DAY1_FIELD, HOUR0_FIELD, HOUR1_FIELD, LONG, MEDIUM, MILLISECOND_FIELD, MINUTE_FIELD, MONTH_FIELD, numberFormat, SECOND_FIELD, SHORT, TIMEZONE_FIELD, WEEK_OF_MONTH_FIELD, WEEK_OF_YEAR_FIELD, YEAR_FIELD
    • Constructor Summary

      Constructors 
      Constructor Description
      CachedDateFormat​(java.text.DateFormat dateFormat, int expiration)
      Creates a new CachedDateFormat object.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static int findMillisecondStart​(long time, java.lang.String formatted, java.text.DateFormat formatter)
      Finds start of millisecond field in formatted time.
      java.lang.StringBuffer format​(long now, java.lang.StringBuffer buf)
      Formats a millisecond count into a date/time string.
      java.lang.StringBuffer format​(java.util.Date date, java.lang.StringBuffer sbuf, java.text.FieldPosition fieldPosition)
      Formats a Date into a date/time string.
      static int getMaximumCacheValidity​(java.lang.String pattern)
      Gets maximum cache validity for the specified SimpleDateTime conversion pattern.
      java.text.NumberFormat getNumberFormat()
      Gets number formatter.
      java.util.Date parse​(java.lang.String s, java.text.ParsePosition pos)
      This method is delegated to the formatter which most likely returns null.
      void setTimeZone​(java.util.TimeZone timeZone)
      Set timezone.
      • Methods inherited from class java.text.DateFormat

        clone, equals, format, format, getAvailableLocales, getCalendar, getDateInstance, getDateInstance, getDateInstance, getDateTimeInstance, getDateTimeInstance, getDateTimeInstance, getInstance, getTimeInstance, getTimeInstance, getTimeInstance, getTimeZone, hashCode, isLenient, parse, parseObject, setCalendar, setLenient, setNumberFormat
      • Methods inherited from class java.text.Format

        format, formatToCharacterIterator, parseObject
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • NO_MILLISECONDS

        public static final int NO_MILLISECONDS
        Constant used to represent that there was no change observed when changing the millisecond count.
        See Also:
        Constant Field Values
      • UNRECOGNIZED_MILLISECONDS

        public static final int UNRECOGNIZED_MILLISECONDS
        Constant used to represent that there was an observed change, but was an expected change.
        See Also:
        Constant Field Values
    • Constructor Detail

      • CachedDateFormat

        public CachedDateFormat​(java.text.DateFormat dateFormat,
                                int expiration)
        Creates a new CachedDateFormat object.
        Parameters:
        dateFormat - Date format, may not be null.
        expiration - maximum cached range in milliseconds. If the dateFormat is known to be incompatible with the caching algorithm, use a value of 0 to totally disable caching or 1 to only use cache for duplicate requests.
    • Method Detail

      • findMillisecondStart

        public static int findMillisecondStart​(long time,
                                               java.lang.String formatted,
                                               java.text.DateFormat formatter)
        Finds start of millisecond field in formatted time.
        Parameters:
        time - long time, must be integral number of seconds
        formatted - String corresponding formatted string
        formatter - DateFormat date format
        Returns:
        int position in string of first digit of milliseconds, -1 indicates no millisecond field, -2 indicates unrecognized field (likely RelativeTimeDateFormat)
      • format

        public java.lang.StringBuffer format​(java.util.Date date,
                                             java.lang.StringBuffer sbuf,
                                             java.text.FieldPosition fieldPosition)
        Formats a Date into a date/time string.
        Specified by:
        format in class java.text.DateFormat
        Parameters:
        date - the date to format.
        sbuf - the string buffer to write to.
        fieldPosition - remains untouched.
        Returns:
        the formatted time string.
      • format

        public java.lang.StringBuffer format​(long now,
                                             java.lang.StringBuffer buf)
        Formats a millisecond count into a date/time string.
        Parameters:
        now - Number of milliseconds after midnight 1 Jan 1970 GMT.
        buf - the string buffer to write to.
        Returns:
        the formatted time string.
      • setTimeZone

        public void setTimeZone​(java.util.TimeZone timeZone)
        Set timezone. Setting the timezone using getCalendar().setTimeZone() will likely cause caching to misbehave.
        Overrides:
        setTimeZone in class java.text.DateFormat
        Parameters:
        timeZone - TimeZone new timezone
      • parse

        public java.util.Date parse​(java.lang.String s,
                                    java.text.ParsePosition pos)
        This method is delegated to the formatter which most likely returns null.
        Specified by:
        parse in class java.text.DateFormat
        Parameters:
        s - string representation of date.
        pos - field position, unused.
        Returns:
        parsed date, likely null.
      • getNumberFormat

        public java.text.NumberFormat getNumberFormat()
        Gets number formatter.
        Overrides:
        getNumberFormat in class java.text.DateFormat
        Returns:
        NumberFormat number formatter
      • getMaximumCacheValidity

        public static int getMaximumCacheValidity​(java.lang.String pattern)
        Gets maximum cache validity for the specified SimpleDateTime conversion pattern.
        Parameters:
        pattern - conversion pattern, may not be null.
        Returns:
        Duration in milliseconds from an integral second that the cache will return consistent results.