|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.text.Format org.apache.commons.lang3.time.FastDateFormat
public class FastDateFormat
FastDateFormat is a fast and thread-safe version of
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 and some year patterns - 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 incompatibility with Java 1.4, but at a gain of
useful functionality.
Javadoc cites for the year pattern: For formatting, if the number of pattern letters is 2, the year is truncated to 2 digits; otherwise it is interpreted as a number. Starting with Java 1.7 a pattern of 'Y' or 'YYY' will be formatted as '2003', while it was '03' in former Java versions. FastDateFormat implements the behavior of Java 7.
Nested Class Summary |
---|
Nested classes/interfaces inherited from class java.text.Format |
---|
Format.Field |
Field Summary | |
---|---|
static int |
FULL
FULL locale dependent date or time style. |
static int |
LONG
LONG locale dependent date or time style. |
static int |
MEDIUM
MEDIUM locale dependent date or time style. |
static int |
SHORT
SHORT locale dependent date or time style. |
Constructor Summary | |
---|---|
protected |
FastDateFormat(String pattern,
TimeZone timeZone,
Locale locale)
Constructs a new FastDateFormat. |
Method Summary | |
---|---|
protected StringBuffer |
applyRules(Calendar calendar,
StringBuffer buf)
Performs the formatting by applying the rules to the specified calendar. |
boolean |
equals(Object obj)
Compares two objects for equality. |
String |
format(Calendar calendar)
Formats a Calendar object. |
StringBuffer |
format(Calendar calendar,
StringBuffer buf)
Formats a Calendar object into the
supplied StringBuffer . |
String |
format(Date date)
Formats a Date object using a GregorianCalendar . |
StringBuffer |
format(Date date,
StringBuffer buf)
Formats a Date object into the
supplied StringBuffer using a GregorianCalendar . |
String |
format(long millis)
Formats a millisecond long value. |
StringBuffer |
format(long millis,
StringBuffer buf)
Formats a milliseond long value into the
supplied StringBuffer . |
StringBuffer |
format(Object obj,
StringBuffer toAppendTo,
FieldPosition pos)
Formats a Date , Calendar or
Long (milliseconds) object. |
static FastDateFormat |
getDateInstance(int style)
Gets a date formatter instance using the specified style in the default time zone and locale. |
static FastDateFormat |
getDateInstance(int style,
Locale locale)
Gets a date formatter instance using the specified style and locale in the default time zone. |
static FastDateFormat |
getDateInstance(int style,
TimeZone timeZone)
Gets a date formatter instance using the specified style and time zone in the default locale. |
static FastDateFormat |
getDateInstance(int style,
TimeZone timeZone,
Locale locale)
Gets a date formatter instance using the specified style, time zone and locale. |
static FastDateFormat |
getDateTimeInstance(int dateStyle,
int timeStyle)
Gets a date/time formatter instance using the specified style in the default time zone and locale. |
static FastDateFormat |
getDateTimeInstance(int dateStyle,
int timeStyle,
Locale locale)
Gets a date/time formatter instance using the specified style and locale in the default time zone. |
static FastDateFormat |
getDateTimeInstance(int dateStyle,
int timeStyle,
TimeZone timeZone)
Gets a date/time formatter instance using the specified style and time zone in the default locale. |
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. |
static FastDateFormat |
getInstance()
Gets a formatter instance using the default pattern in the default locale. |
static FastDateFormat |
getInstance(String pattern)
Gets a formatter instance using the specified pattern in the default locale. |
static FastDateFormat |
getInstance(String pattern,
Locale locale)
Gets a formatter instance using the specified pattern and locale. |
static FastDateFormat |
getInstance(String pattern,
TimeZone timeZone)
Gets a formatter instance using the specified pattern and time zone. |
static FastDateFormat |
getInstance(String pattern,
TimeZone timeZone,
Locale locale)
Gets a formatter instance using the specified pattern, time zone and locale. |
Locale |
getLocale()
Gets the locale used by this formatter. |
int |
getMaxLengthEstimate()
Gets an estimate for the maximum string length that the formatter will produce. |
String |
getPattern()
Gets the pattern used by this formatter. |
static FastDateFormat |
getTimeInstance(int style)
Gets a time formatter instance using the specified style in the default time zone and locale. |
static FastDateFormat |
getTimeInstance(int style,
Locale locale)
Gets a time formatter instance using the specified style and locale in the default time zone. |
static FastDateFormat |
getTimeInstance(int style,
TimeZone timeZone)
Gets a time formatter instance using the specified style and time zone in the default locale. |
static FastDateFormat |
getTimeInstance(int style,
TimeZone timeZone,
Locale locale)
Gets a time formatter instance using the specified style, time zone and locale. |
TimeZone |
getTimeZone()
Gets the time zone used by this formatter. |
int |
hashCode()
Returns a hashcode compatible with equals. |
Object |
parseObject(String source,
ParsePosition pos)
Parsing is not supported. |
protected List<org.apache.commons.lang3.time.FastDateFormat.Rule> |
parsePattern()
Returns a list of Rules given a pattern. |
protected String |
parseToken(String pattern,
int[] indexRef)
Performs the parsing of tokens. |
protected org.apache.commons.lang3.time.FastDateFormat.NumberRule |
selectNumberRule(int field,
int padding)
Gets an appropriate rule for the padding required. |
String |
toString()
Gets a debugging string version of this formatter. |
Methods inherited from class java.text.Format |
---|
clone, format, formatToCharacterIterator, parseObject |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final int FULL
public static final int LONG
public static final int MEDIUM
public static final int SHORT
Constructor Detail |
---|
protected FastDateFormat(String pattern, TimeZone timeZone, Locale locale)
Constructs a new FastDateFormat.
pattern
- SimpleDateFormat
compatible patterntimeZone
- non-null time zone to uselocale
- non-null locale to use
NullPointerException
- if pattern, timeZone, or locale is null.Method Detail |
---|
public static FastDateFormat getInstance()
Gets a formatter instance using the default pattern in the default locale.
public static FastDateFormat getInstance(String pattern)
Gets a formatter instance using the specified pattern in the default locale.
pattern
- SimpleDateFormat
compatible
pattern
IllegalArgumentException
- if pattern is invalidpublic static FastDateFormat getInstance(String pattern, TimeZone timeZone)
Gets a formatter instance using the specified pattern and time zone.
pattern
- SimpleDateFormat
compatible
patterntimeZone
- optional time zone, overrides time zone of
formatted date
IllegalArgumentException
- if pattern is invalidpublic static FastDateFormat getInstance(String pattern, Locale locale)
Gets a formatter instance using the specified pattern and locale.
pattern
- SimpleDateFormat
compatible
patternlocale
- optional locale, overrides system locale
IllegalArgumentException
- if pattern is invalidpublic static FastDateFormat getInstance(String pattern, TimeZone timeZone, Locale locale)
Gets a formatter instance using the specified pattern, time zone and locale.
pattern
- SimpleDateFormat
compatible
patterntimeZone
- optional time zone, overrides time zone of
formatted datelocale
- optional locale, overrides system locale
IllegalArgumentException
- if pattern is invalid
or null
public static FastDateFormat getDateInstance(int style)
Gets a date formatter instance using the specified style in the default time zone and locale.
style
- date style: FULL, LONG, MEDIUM, or SHORT
IllegalArgumentException
- if the Locale has no date
pattern definedpublic static FastDateFormat getDateInstance(int style, Locale locale)
Gets a date formatter instance using the specified style and locale in the default time zone.
style
- date style: FULL, LONG, MEDIUM, or SHORTlocale
- optional locale, overrides system locale
IllegalArgumentException
- if the Locale has no date
pattern definedpublic static FastDateFormat getDateInstance(int style, TimeZone timeZone)
Gets a date formatter instance using the specified style and time zone in the default locale.
style
- date style: FULL, LONG, MEDIUM, or SHORTtimeZone
- optional time zone, overrides time zone of
formatted date
IllegalArgumentException
- if the Locale has no date
pattern definedpublic 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
IllegalArgumentException
- if the Locale has no date
pattern definedpublic static FastDateFormat getTimeInstance(int style)
Gets a time formatter instance using the specified style in the default time zone and locale.
style
- time style: FULL, LONG, MEDIUM, or SHORT
IllegalArgumentException
- if the Locale has no time
pattern definedpublic static FastDateFormat getTimeInstance(int style, Locale locale)
Gets a time formatter instance using the specified style and locale in the default time zone.
style
- time style: FULL, LONG, MEDIUM, or SHORTlocale
- optional locale, overrides system locale
IllegalArgumentException
- if the Locale has no time
pattern definedpublic static FastDateFormat getTimeInstance(int style, TimeZone timeZone)
Gets a time formatter instance using the specified style and time zone in the default locale.
style
- time style: FULL, LONG, MEDIUM, or SHORTtimeZone
- optional time zone, overrides time zone of
formatted time
IllegalArgumentException
- if the Locale has no time
pattern definedpublic 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
IllegalArgumentException
- if the Locale has no time
pattern definedpublic static FastDateFormat getDateTimeInstance(int dateStyle, int timeStyle)
Gets a date/time formatter instance using the specified style in the default time zone and locale.
dateStyle
- date style: FULL, LONG, MEDIUM, or SHORTtimeStyle
- time style: FULL, LONG, MEDIUM, or SHORT
IllegalArgumentException
- if the Locale has no date/time
pattern definedpublic static FastDateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)
Gets a date/time formatter instance using the specified style and locale in the default time zone.
dateStyle
- date style: FULL, LONG, MEDIUM, or SHORTtimeStyle
- time style: FULL, LONG, MEDIUM, or SHORTlocale
- optional locale, overrides system locale
IllegalArgumentException
- if the Locale has no date/time
pattern definedpublic static FastDateFormat getDateTimeInstance(int dateStyle, int timeStyle, TimeZone timeZone)
Gets a date/time formatter instance using the specified style and time zone in the default locale.
dateStyle
- date style: FULL, LONG, MEDIUM, or SHORTtimeStyle
- time style: FULL, LONG, MEDIUM, or SHORTtimeZone
- optional time zone, overrides time zone of
formatted date
IllegalArgumentException
- if the Locale has no date/time
pattern definedpublic 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
IllegalArgumentException
- if the Locale has no date/time
pattern definedprotected List<org.apache.commons.lang3.time.FastDateFormat.Rule> parsePattern()
Returns a list of Rules given a pattern.
List
of Rule objects
IllegalArgumentException
- if pattern is invalidprotected String parseToken(String pattern, int[] indexRef)
Performs the parsing of tokens.
pattern
- the patternindexRef
- index references
protected org.apache.commons.lang3.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
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
Formats a Date
, Calendar
or
Long
(milliseconds) object.
format
in class Format
obj
- the object to formattoAppendTo
- the buffer to append topos
- the position - ignored
public String format(long millis)
Formats a millisecond long
value.
millis
- the millisecond value to format
public String format(Date date)
Formats a Date
object using a GregorianCalendar
.
date
- the date to format
public String format(Calendar calendar)
Formats a Calendar
object.
calendar
- the calendar to format
public StringBuffer format(long millis, StringBuffer buf)
Formats a milliseond long
value into the
supplied StringBuffer
.
millis
- the millisecond value to formatbuf
- the buffer to format into
public StringBuffer format(Date date, StringBuffer buf)
Formats a Date
object into the
supplied StringBuffer
using a GregorianCalendar
.
date
- the date to formatbuf
- the buffer to format into
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
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
public Object parseObject(String source, ParsePosition pos)
Parsing is not supported.
parseObject
in class Format
source
- the string to parsepos
- the parsing position
null
as not supportedpublic String getPattern()
Gets the pattern used by this formatter.
SimpleDateFormat
compatiblepublic TimeZone getTimeZone()
Gets the time zone used by this formatter.
This zone is always used for Date
formatting.
public Locale getLocale()
Gets the locale used by this formatter.
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.
public boolean equals(Object obj)
Compares two objects for equality.
equals
in class Object
obj
- the object to compare to
true
if equalpublic int hashCode()
Returns a hashcode compatible with equals.
hashCode
in class Object
public String toString()
Gets a debugging string version of this formatter.
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |