Top Description Fields Constructors Methods
org.apache.commons.lang3.time

public Class FastDateFormat

extends Format
implements DateParser, DatePrinter
Class Inheritance
All Implemented Interfaces
org.apache.commons.lang3.time.DatePrinter, org.apache.commons.lang3.time.DateParser
Imports
java.text.DateFormat, .FieldPosition, .Format, .ParseException, .ParsePosition, .SimpleDateFormat, java.util.Calendar, .Date, .GregorianCalendar, .Locale, .TimeZone

FastDateFormat is a fast and thread-safe version of java.text.SimpleDateFormat.

To obtain an instance of FastDateFormat, use one of the static factory methods: getInstance(String, TimeZone, Locale), getDateInstance(int, TimeZone, Locale), getTimeInstance(int, TimeZone, Locale), or getDateTimeInstance(int, int, TimeZone, Locale)

Since FastDateFormat is thread safe, you can use a static member instance:

private static final FastDateFormat DATE_FORMATTER = FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.SHORT);

This class can be used as a direct replacement to SimpleDateFormat in most formatting and parsing 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.

All patterns are compatible with SimpleDateFormat (except time zones and some year patterns - see below).

Since 3.2, FastDateFormat supports parsing as well as printing.

Java 1.4 introduced a new pattern letter, 'Z', to represent time zones in RFC822 format (for example, +0800 or -1100). This pattern letter can be used here (on all JDK versions).

In addition, the pattern 'ZZ' has been made to represent ISO 8601 extended format time zones (for example, +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.

Since
2.0

Field Summary

Modifier and TypeField and Description
private static final AbstractFormatCache<FastDateFormat>
public static final int
FULL

FULL locale dependent date or time style.

public static final int
LONG

LONG locale dependent date or time style.

public static final int
MEDIUM

MEDIUM locale dependent date or time style.

private final FastDateParser
parser

Our fast parser.

private final FastDatePrinter
printer

Our fast printer.

private static final long
serialVersionUID

Hides java.text.Format.serialVersionUID.

Required for serialization support.
public static final int
SHORT

SHORT locale dependent date or time style.

Constructor Summary

AccessConstructor and Description
protected
FastDateFormat(final String
java.text.SimpleDateFormat compatible pattern.
pattern
,
final TimeZone
non-null time zone to use.
timeZone
,
final Locale
non-null locale to use.
locale
)

Constructs a new FastDateFormat.

protected
FastDateFormat(final String
java.text.SimpleDateFormat compatible pattern.
pattern
,
final TimeZone
non-null time zone to use.
timeZone
,
final Locale
non-null locale to use.
locale
,
final Date
The start of the 100-year period to use as the "default century" for 2 digit year parsing. If centuryStart is null, defaults to now - 80 years.
centuryStart
)

Constructs a new FastDateFormat.

Method Summary

Modifier and TypeMethod and Description
protected StringBuffer

Returns:

the specified string buffer.
applyRules
(final Calendar
the calendar to format.
calendar
,
final StringBuffer
the buffer to format into.
buf
)

Performs the formatting by applying the rules to the specified calendar.

pack-priv static void
clear()

Clears the cache.

public boolean

Returns:

true if equal.
equals
(final Object
the object to compare to.
obj
)

Overrides java.lang.Object.equals.

Compares two objects for equality.

public String

Returns:

the formatted string.
format
(final Calendar
the calendar to format.
calendar
)

Implements org.apache.commons.lang3.time.DatePrinter.format.

Formats a Calendar object.

public <B extends Appendable> B

Returns:

the specified string buffer.
format
(final Calendar
the calendar to format.
calendar
,
final B
the buffer to format into.
buf
)

Implements org.apache.commons.lang3.time.DatePrinter.format.

Formats a Calendar object into the supplied StringBuffer.

public StringBuffer

Returns:

the specified string buffer.
format
(final Calendar
the calendar to format.
calendar
,
final StringBuffer
the buffer to format into.
buf
)

Implements org.apache.commons.lang3.time.DatePrinter.format, org.apache.commons.lang3.time.DatePrinter.format.

Deprecated Use {format(Calendar, Appendable).

Formats a Calendar object into the supplied StringBuffer.

public String

Returns:

the formatted string.
format
(final Date
the date to format.
date
)

Implements org.apache.commons.lang3.time.DatePrinter.format.

Formats a Date object using a GregorianCalendar.

public <B extends Appendable> B

Returns:

the specified string buffer.
format
(final Date
the date to format.
date
,
final B
the buffer to format into.
buf
)

Implements org.apache.commons.lang3.time.DatePrinter.format.

Formats a Date object into the supplied StringBuffer using a GregorianCalendar.

public StringBuffer

Returns:

the specified string buffer.
format
(final Date
the date to format.
date
,
final StringBuffer
the buffer to format into.
buf
)

Implements org.apache.commons.lang3.time.DatePrinter.format, org.apache.commons.lang3.time.DatePrinter.format.

Deprecated Use {format(Date, Appendable).

Formats a Date object into the supplied StringBuffer using a GregorianCalendar.

public String

Returns:

the formatted string.
format
(final long
the millisecond value to format.
millis
)

Implements org.apache.commons.lang3.time.DatePrinter.format.

Formats a millisecond long value.

public <B extends Appendable> B

Returns:

the specified string buffer.
format
(final long
the millisecond value to format.
millis
,
final B
the buffer to format into.
buf
)

Implements org.apache.commons.lang3.time.DatePrinter.format.

Formats a millisecond long value into the supplied StringBuffer.

public StringBuffer

Returns:

the specified string buffer.
format
(final long
the millisecond value to format.
millis
,
final StringBuffer
the buffer to format into.
buf
)

Implements org.apache.commons.lang3.time.DatePrinter.format, org.apache.commons.lang3.time.DatePrinter.format.

Deprecated Use {format(long, Appendable).

Formats a millisecond long value into the supplied StringBuffer.

public StringBuffer

Returns:

the given buffer.
format
(final Object
the object to format.
obj
,
final StringBuffer
the buffer to append to.
toAppendTo
,
final FieldPosition
the position, ignored.
pos
)

Implements abstract java.text.Format.format.

Implements org.apache.commons.lang3.time.DatePrinter.format.

Formats a Date, Calendar or Long (milliseconds) object.

public static FastDateFormat

Returns:

a localized standard date formatter.
getDateInstance
(final int
date style: FULL, LONG, MEDIUM, or SHORT.
style
)

Gets a date formatter instance using the specified style in the default time zone and locale.

public static FastDateFormat

Returns:

a localized standard date formatter.
getDateInstance
(final int
date style: FULL, LOFULL,MEDIUM, or SHORT.
style
,
final Locale
optional locale, overrides system locale.
locale
)

Gets a date formatter instance using the specified style and locale in the default time zone.

public static FastDateFormat

Returns:

a localized standard date formatter.
getDateInstance
(final int
date style: FULL, LONG, MEDIUM, or SHORT.
style
,
final TimeZone
optional time zone, overrides time zone of formatted date.
timeZone
)

Gets a date formatter instance using the specified style and time zone in the default locale.

public static FastDateFormat

Returns:

a localized standard date formatter.
getDateInstance
(final int
date style: FULL, LONG, MEDIUM, or SHORT.
style
,
final TimeZone
optional time zone, overrides time zone of formatted date.
timeZone
,
final Locale
optional locale, overrides system locale.
locale
)

Gets a date formatter instance using the specified style, time zone and locale.

public static FastDateFormat

Returns:

a localized standard date/time formatter.
getDateTimeInstance
(final int
date style: FULL, LONG, MEDIUM, or SHORT.
dateStyle
,
final int
time style: FULL, LONG, MEDIUM, or SHORT.
timeStyle
)

Gets a date/time formatter instance using the specified style in the default time zone and locale.

public static FastDateFormat

Returns:

a localized standard date/time formatter.
getDateTimeInstance
(final int
date style: FULL, LONG, MEDIUM, or SHORT.
dateStyle
,
final int
time style: FULL, LONG, MEDIUM, or SHORT.
timeStyle
,
final Locale
optional locale, overrides system locale.
locale
)

Gets a date/time formatter instance using the specified style and locale in the default time zone.

public static FastDateFormat

Returns:

a localized standard date/time formatter.
getDateTimeInstance
(final int
date style: FULL, LONG, MEDIUM, or SHORT.
dateStyle
,
final int
time style: FULL, LONG, MEDIUM, or SHORT.
timeStyle
,
final TimeZone
optional time zone, overrides time zone of formatted date.
timeZone
)

Gets a date/time formatter instance using the specified style and time zone in the default locale.

public static FastDateFormat

Returns:

a localized standard date/time formatter.
getDateTimeInstance
(final int
date style: FULL, LONG, MEDIUM, or SHORT.
dateStyle
,
final int
time style: FULL, LONG, MEDIUM, or SHORT.
timeStyle
,
final TimeZone
optional time zone, overrides time zone of formatted date.
timeZone
,
final Locale
optional locale, overrides system locale.
locale
)

Gets a date/time formatter instance using the specified style, time zone and locale.

public static FastDateFormat

Returns:

a date/time formatter.
getInstance
()

Gets a formatter instance using the default pattern in the default locale.

public static FastDateFormat

Returns:

a pattern based date/time formatter.
getInstance
(final String
java.text.SimpleDateFormat compatible pattern.
pattern
)

Gets a formatter instance using the specified pattern in the default locale and time zone.

public static FastDateFormat

Returns:

a pattern based date/time formatter.
getInstance
(final String
java.text.SimpleDateFormat compatible pattern.
pattern
,
final Locale
optional locale, overrides system locale.
locale
)

Gets a formatter instance using the specified pattern and locale using the default time zone.

public static FastDateFormat

Returns:

a pattern based date/time formatter.
getInstance
(final String
java.text.SimpleDateFormat compatible pattern.
pattern
,
final TimeZone
optional time zone, overrides time zone of formatted date.
timeZone
)

Gets a formatter instance using the specified pattern and time zone.

public static FastDateFormat

Returns:

a pattern based date/time formatter.
getInstance
(final String
java.text.SimpleDateFormat compatible pattern.
pattern
,
final TimeZone
optional time zone, overrides time zone of formatted date.
timeZone
,
final Locale
optional locale, overrides system locale.
locale
)

Gets a formatter instance using the specified pattern, time zone and locale.

public Locale

Returns:

the locale.
getLocale
()

Implements org.apache.commons.lang3.time.DateParser.getLocale, org.apache.commons.lang3.time.DatePrinter.getLocale.

Gets the locale used by this formatter.

public int

Returns:

the maximum formatted length.
getMaxLengthEstimate
()

Gets an estimate for the maximum string length that the formatter will produce.

public String
public static FastDateFormat

Returns:

a localized standard time formatter.
getTimeInstance
(final int
time style: FULL, LONG, MEDIUM, or SHORT.
style
)

Gets a time formatter instance using the specified style in the default time zone and locale.

public static FastDateFormat

Returns:

a localized standard time formatter.
getTimeInstance
(final int
time style: FULL, LONG, MEDIUM, or SHORT.
style
,
final Locale
optional locale, overrides system locale.
locale
)

Gets a time formatter instance using the specified style and locale in the default time zone.

public static FastDateFormat

Returns:

a localized standard time formatter.
getTimeInstance
(final int
time style: FULL, LONG, MEDIUM, or SHORT.
style
,
final TimeZone
optional time zone, overrides time zone of formatted time.
timeZone
)

Gets a time formatter instance using the specified style and time zone in the default locale.

public static FastDateFormat

Returns:

a localized standard time formatter.
getTimeInstance
(final int
time style: FULL, LONG, MEDIUM, or SHORT.
style
,
final TimeZone
optional time zone, overrides time zone of formatted time.
timeZone
,
final Locale
optional locale, overrides system locale.
locale
)

Gets a time formatter instance using the specified style, time zone and locale.

public TimeZone

Returns:

the time zone.
getTimeZone
()

Implements org.apache.commons.lang3.time.DateParser.getTimeZone, org.apache.commons.lang3.time.DatePrinter.getTimeZone.

Gets the time zone used by this formatter.

public int

Returns:

a hash code compatible with equals.
hashCode
()

Overrides java.lang.Object.hashCode.

Returns a hash code compatible with equals.

public Date
parse(final String source)

Implements org.apache.commons.lang3.time.DateParser.parse.

Equivalent to DateFormat.parse(String).

public Date
parse(final String source, final ParsePosition pos)

Implements org.apache.commons.lang3.time.DateParser.parse.

Equivalent to DateFormat.parse(String, ParsePosition).

public boolean
parse(final String source, final ParsePosition pos, final Calendar calendar)

Implements org.apache.commons.lang3.time.DateParser.parse.

Parses a formatted date string according to the format.

public Object
parseObject(final String source, final ParsePosition pos)

Implements abstract java.text.Format.parseObject.

Implements org.apache.commons.lang3.time.DateParser.parseObject.

Parses a date/time string according to the given parse position.

public String

Returns:

a debug string.
toString
()

Overrides java.lang.Object.toString.

Gets a debugging string version of this formatter.

Inherited from java.text.Format:
cloneformatformatToCharacterIteratorparseObject