public final class

CachedDateFormat

extends DateFormat
java.lang.Object
   ↳ java.text.Format
     ↳ java.text.DateFormat
       ↳ org.apache.log4j.pattern.CachedDateFormat

Class Overview

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.

Summary

Constants
int NO_MILLISECONDS Constant used to represent that there was no change observed when changing the millisecond count.
int UNRECOGNIZED_MILLISECONDS Constant used to represent that there was an observed change, but was an expected change.
[Expand]
Inherited Constants
From class java.text.DateFormat
[Expand]
Inherited Fields
From class java.text.DateFormat
Public Constructors
CachedDateFormat(DateFormat dateFormat, int expiration)
Creates a new CachedDateFormat object.
Public Methods
static int findMillisecondStart(long time, String formatted, DateFormat formatter)
Finds start of millisecond field in formatted time.
StringBuffer format(Date date, StringBuffer sbuf, FieldPosition fieldPosition)
Formats a Date into a date/time string.
StringBuffer format(long now, StringBuffer buf)
Formats a millisecond count into a date/time string.
static int getMaximumCacheValidity(String pattern)
Gets maximum cache validity for the specified SimpleDateTime conversion pattern.
NumberFormat getNumberFormat()
Gets number formatter.
Date parse(String s, ParsePosition pos)
This method is delegated to the formatter which most likely returns null.
void setTimeZone(TimeZone timeZone)
Set timezone.
[Expand]
Inherited Methods
From class java.text.DateFormat
From class java.text.Format
From class java.lang.Object

Constants

public static final int NO_MILLISECONDS

Constant used to represent that there was no change observed when changing the millisecond count.

Constant Value: -2 (0xfffffffe)

public static final int UNRECOGNIZED_MILLISECONDS

Constant used to represent that there was an observed change, but was an expected change.

Constant Value: -1 (0xffffffff)

Public Constructors

public CachedDateFormat (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.

Public Methods

public static int findMillisecondStart (long time, String formatted, 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)

public StringBuffer format (Date date, StringBuffer sbuf, FieldPosition fieldPosition)

Formats a Date into a date/time string.

Parameters
date the date to format.
sbuf the string buffer to write to.
fieldPosition remains untouched.
Returns
  • the formatted time string.

public StringBuffer format (long now, 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.

public static int getMaximumCacheValidity (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.

public NumberFormat getNumberFormat ()

Gets number formatter.

Returns
  • NumberFormat number formatter

public Date parse (String s, ParsePosition pos)

This method is delegated to the formatter which most likely returns null.

Parameters
s string representation of date.
pos field position, unused.
Returns
  • parsed date, likely null.

public void setTimeZone (TimeZone timeZone)

Set timezone. Setting the timezone using getCalendar().setTimeZone() will likely cause caching to misbehave.

Parameters
timeZone TimeZone new timezone