FastDateFormat is a fast and thread-safe version of
java.text.SimpleDateFormat
.
This class can be used as a direct replacement to
SimpleDateFormat
in most formatting situations.
This class is especially useful in multi-threaded server environments.
SimpleDateFormat
is not thread-safe in any JDK version,
nor will it be as Sun have closed the bug/RFE.
Only formatting is supported, but all patterns are compatible with
SimpleDateFormat (except time zones - see below).
Java 1.4 introduced a new pattern letter,
'Z'
, to represent
time zones in RFC822 format (eg.
+0800
or
-1100
).
This pattern letter can be used here (on all JDK versions).
In addition, the pattern
'ZZ'
has been made to represent
ISO8601 full format time zones (eg.
+08:00
or
-11:00
).
This introduces a minor incompatability with Java 1.4, but at a gain of
useful functionality.
applyRules
protected StringBuffer applyRules(Calendar calendar,
StringBuffer buf)
Performs the formatting by applying the rules to the
specified calendar.
calendar
- the calendar to formatbuf
- the buffer to format into
- the specified string buffer
equals
public boolean equals(Object obj)
Compare two objects for equality.
obj
- the object to compare to
true
if equal
format
public String format(Calendar calendar)
Formats a Calendar
object.
calendar
- the calendar to format
- the formatted string
format
public StringBuffer format(Calendar calendar,
StringBuffer buf)
Formats a Calendar
object into the
supplied StringBuffer
.
calendar
- the calendar to formatbuf
- the buffer to format into
- the specified string buffer
format
public String format(Date date)
Formats a Date
object.
date
- the date to format
- the formatted string
format
public StringBuffer format(Date date,
StringBuffer buf)
Formats a Date
object into the
supplied StringBuffer
.
date
- the date to formatbuf
- the buffer to format into
- the specified string buffer
format
public StringBuffer format(Object obj,
StringBuffer toAppendTo,
FieldPosition pos)
Format either a Date
or a
Calendar
object.
obj
- the object to formattoAppendTo
- the buffer to append topos
- the position - ignored
- the buffer passed in
getDateInstance
public static FastDateFormat getDateInstance(int style,
TimeZone timeZone,
Locale locale)
Gets a date formatter instance using the specified style, time
zone and locale.
style
- date style: FULL, LONG, MEDIUM, or SHORTtimeZone
- optional time zone, overrides time zone of
formatted datelocale
- optional locale, overrides system locale
- a localized standard date formatter
getDateTimeInstance
public static FastDateFormat getDateTimeInstance(int dateStyle,
int timeStyle,
TimeZone timeZone,
Locale locale)
Gets a date/time formatter instance using the specified style,
time zone and locale.
dateStyle
- date style: FULL, LONG, MEDIUM, or SHORTtimeStyle
- time style: FULL, LONG, MEDIUM, or SHORTtimeZone
- optional time zone, overrides time zone of
formatted datelocale
- optional locale, overrides system locale
- a localized standard date/time formatter
getInstance
public static FastDateFormat getInstance()
Gets a formatter instance using the default pattern in the
default locale.
- a date/time formatter
getInstance
public static FastDateFormat getInstance(String pattern)
Gets a formatter instance using the specified pattern in the
default locale.
pattern
- java.text.SimpleDateFormat
compatible
pattern
- a pattern based date/time formatter
getInstance
public static FastDateFormat getInstance(String pattern,
Locale locale)
Gets a formatter instance using the specified pattern and
locale.
pattern
- java.text.SimpleDateFormat
compatible
patternlocale
- optional locale, overrides system locale
- a pattern based date/time formatter
getInstance
public static FastDateFormat getInstance(String pattern,
TimeZone timeZone)
Gets a formatter instance using the specified pattern and
time zone.
pattern
- java.text.SimpleDateFormat
compatible
patterntimeZone
- optional time zone, overrides time zone of
formatted date
- a pattern based date/time formatter
getInstance
public static FastDateFormat getInstance(String pattern,
TimeZone timeZone,
Locale locale)
Gets a formatter instance using the specified pattern, time zone
and locale.
pattern
- java.text.SimpleDateFormat
compatible
patterntimeZone
- optional time zone, overrides time zone of
formatted datelocale
- optional locale, overrides system locale
- a pattern based date/time formatter
getLocale
public Locale getLocale()
Gets the locale used by this formatter.
- the locale
getMaxLengthEstimate
public int getMaxLengthEstimate()
Gets an estimate for the maximum string length that the
formatter will produce.
The actual formatted length will almost always be less than or
equal to this amount.
- the maximum formatted length
getPattern
public String getPattern()
Gets the pattern used by this formatter.
- the pattern,
java.text.SimpleDateFormat
compatible
getTimeInstance
public static FastDateFormat getTimeInstance(int style,
TimeZone timeZone,
Locale locale)
Gets a time formatter instance using the specified style, time
zone and locale.
style
- time style: FULL, LONG, MEDIUM, or SHORTtimeZone
- optional time zone, overrides time zone of
formatted timelocale
- optional locale, overrides system locale
- a localized standard time formatter
getTimeZone
public TimeZone getTimeZone()
Gets the time zone used by this formatter.
This zone is always used for
Date
formatting.
If a
Calendar
is passed in to be formatted, the
time zone on that may be used depending on
getTimeZoneOverridesCalendar()
.
- the time zone
getTimeZoneOverridesCalendar
public boolean getTimeZoneOverridesCalendar()
Returns true
if the time zone of the
calendar overrides the time zone of the formatter.
true
if time zone of formatter
overridden for calendars
hashCode
public int hashCode()
A suitable hashcode.
- a hashcode compatable with equals
init
protected void init()
Initialise the instance for first use.
parseObject
public Object parseObject(String source,
ParsePosition pos)
Parsing not supported.
source
- the string to parsepos
- the parsing position
null
as not supported
parsePattern
protected List parsePattern()
Returns a list of Rules given a pattern.
- a
List
of Rule objects
parseToken
protected String parseToken(String pattern,
int[] indexRef)
Performs the parsing of tokens.
pattern
- the patternindexRef
- index references
- parsed token
selectNumberRule
protected org.apache.commons.lang.time.FastDateFormat.NumberRule selectNumberRule(int field,
int padding)
Gets an appropriate rule for the padding required.
field
- the field to get a rule forpadding
- the padding required
- a new rule with the correct padding
toString
public String toString()
Gets a debugging string version of this formatter.
- a debugging string