Class CachedDateFormat
java.lang.Object
java.text.Format
java.text.DateFormat
org.apache.logging.log4j.core.pattern.CachedDateFormat
- All Implemented Interfaces:
Serializable
,Cloneable
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.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.text.DateFormat
DateFormat.Field
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
private final StringBuffer
Cache of previous conversion.private static final int
private static final String
Supported digit set.private final int
Maximum validity period for the cache.private final DateFormat
Wrapped formatter.private static final int
First magic number used to detect the millisecond position.private static final int
Second magic number used to detect the millisecond position.private static final String
Expected representation of first magic number.private static final String
Expected representation of second magic number.private int
Index of initial digit of millisecond pattern or UNRECOGNIZED_MILLISECONDS or NO_MILLISECONDS.static final int
Constant used to represent that there was no change observed when changing the millisecond count.private long
Date requested in previous conversion.private static final long
private long
Integral second preceding the previous converted Date.private static final long
private static final int
private final Date
Scratch date object used to minimize date object creation.private static final int
static final int
Constant used to represent that there was an observed change, but was an expected change.private static final String
Expected representation of 0 milliseconds.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
ConstructorsConstructorDescriptionCachedDateFormat
(DateFormat dateFormat, int expiration) Creates a new CachedDateFormat object. -
Method Summary
Modifier and TypeMethodDescriptionstatic int
findMillisecondStart
(long time, String formatted, DateFormat formatter) Finds start of millisecond field in formatted time.format
(long now, StringBuffer buf) Formats a millisecond count into a date/time string.format
(Date date, StringBuffer sbuf, FieldPosition fieldPosition) Formats a Date into a date/time string.static int
getMaximumCacheValidity
(String pattern) Gets maximum cache validity for the specified SimpleDateTime conversion pattern.Gets number formatter.private static void
millisecondFormat
(int millis, StringBuffer buf, int offset) Formats a count of milliseconds (0-999) into a numeric representation.parse
(String s, ParsePosition pos) This method is delegated to the formatter which most likely returns null.void
setTimeZone
(TimeZone timeZone) Sets the time zone.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
-
Field Details
-
NO_MILLISECONDS
public static final int NO_MILLISECONDSConstant used to represent that there was no change observed when changing the millisecond count.- See Also:
-
UNRECOGNIZED_MILLISECONDS
public static final int UNRECOGNIZED_MILLISECONDSConstant used to represent that there was an observed change, but was an expected change.- See Also:
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
DIGITS
Supported digit set. If the wrapped DateFormat uses a different unit set, the millisecond pattern will not be recognized and duplicate requests will use the cache.- See Also:
-
MAGIC1
private static final int MAGIC1First magic number used to detect the millisecond position.- See Also:
-
MAGICSTRING1
Expected representation of first magic number.- See Also:
-
MAGIC2
private static final int MAGIC2Second magic number used to detect the millisecond position.- See Also:
-
MAGICSTRING2
Expected representation of second magic number.- See Also:
-
ZERO_STRING
Expected representation of 0 milliseconds.- See Also:
-
BUF_SIZE
private static final int BUF_SIZE- See Also:
-
DEFAULT_VALIDITY
private static final int DEFAULT_VALIDITY- See Also:
-
THREE_DIGITS
private static final int THREE_DIGITS- See Also:
-
TWO_DIGITS
private static final int TWO_DIGITS- See Also:
-
SLOTS
private static final long SLOTS- See Also:
-
formatter
Wrapped formatter. -
millisecondStart
private int millisecondStartIndex of initial digit of millisecond pattern or UNRECOGNIZED_MILLISECONDS or NO_MILLISECONDS. -
slotBegin
private long slotBeginIntegral second preceding the previous converted Date. -
cache
Cache of previous conversion. -
expiration
private final int expirationMaximum validity period for the cache. Typically 1, use cache for duplicate requests only, or 1000, use cache for requests within the same integral second. -
previousTime
private long previousTimeDate requested in previous conversion. -
tmpDate
Scratch date object used to minimize date object creation.
-
-
Constructor Details
-
CachedDateFormat
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 Details
-
findMillisecondStart
Finds start of millisecond field in formatted time.- Parameters:
time
- long time, must be integral number of secondsformatted
- String corresponding formatted stringformatter
- 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
Formats a Date into a date/time string.- Specified by:
format
in classDateFormat
- Parameters:
date
- the date to format.sbuf
- the string buffer to write to.fieldPosition
- remains untouched.- Returns:
- the formatted time string.
-
format
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.
-
millisecondFormat
Formats a count of milliseconds (0-999) into a numeric representation.- Parameters:
millis
- Millisecond count between 0 and 999.buf
- String buffer, may not be null.offset
- Starting position in buffer, the length of the buffer must be at least offset + 3.
-
setTimeZone
Sets the time zone.Setting the time zone using getCalendar().setTimeZone() will likely cause caching to misbehave.
- Overrides:
setTimeZone
in classDateFormat
- Parameters:
timeZone
- TimeZone new time zone
-
parse
This method is delegated to the formatter which most likely returns null.- Specified by:
parse
in classDateFormat
- Parameters:
s
- string representation of date.pos
- field position, unused.- Returns:
- parsed date, likely null.
-
getNumberFormat
Gets number formatter.- Overrides:
getNumberFormat
in classDateFormat
- Returns:
- NumberFormat number formatter
-
getMaximumCacheValidity
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.
-