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

public Class FastDatePrinter

extends Object
implements DatePrinter, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.apache.commons.lang3.time.DatePrinter
Imports
java.io.IOException, .ObjectInputStream, .Serializable, java.text.DateFormat, .DateFormatSymbols, .FieldPosition, .SimpleDateFormat, java.util.ArrayList, .Calendar, .Date, .List, .Locale, .TimeZone, java.util.concurrent.ConcurrentHashMap, .ConcurrentMap, org.apache.commons.lang3.CharUtils, .ClassUtils, .LocaleUtils, org.apache.commons.lang3.exception.ExceptionUtils

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

To obtain a FastDatePrinter, use FastDateFormat#getInstance(String, TimeZone, Locale) or another variation of the factory methods of FastDateFormat.

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

private static final DatePrinter DATE_PRINTER = FastDateFormat.getInstance("yyyy-MM-dd");

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 by this class, 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 (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.

Starting with JDK7, ISO 8601 support was added using the pattern 'X'. To maintain compatibility, 'ZZ' will continue to be supported, but using one of the 'X' formats is recommended.

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. FastDatePrinter implements the behavior of Java 7.

Since
3.2
See Also
FastDateParser

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
FastDatePrinter.CharacterLiteral

Inner class to output a constant single character.

private static class
FastDatePrinter.DayInWeekField

Inner class to output the numeric day in week.

private static class
FastDatePrinter.Iso8601_Rule

Inner class to output a time zone as a number +/-HHMM or +/-HH:MM.

private static interface
FastDatePrinter.NumberRule

Inner class defining a numeric rule.

private static class
FastDatePrinter.PaddedNumberField

Inner class to output a padded number.

private static interface
FastDatePrinter.Rule

Inner class defining a rule.

private static class
FastDatePrinter.StringLiteral

Inner class to output a constant string.

private static class
FastDatePrinter.TextField

Inner class to output one of a set of values.

private static class
FastDatePrinter.TimeZoneDisplayKey

Inner class that acts as a compound key for time zone names.

private static class
FastDatePrinter.TimeZoneNameRule

Inner class to output a time zone name.

private static class
FastDatePrinter.TimeZoneNumberRule

Inner class to output a time zone as a number +/-HHMM or +/-HH:MM.

private static class
FastDatePrinter.TwelveHourField

Inner class to output the twelve hour field.

private static class
FastDatePrinter.TwentyFourHourField

Inner class to output the twenty four hour field.

private static class
FastDatePrinter.TwoDigitMonthField

Inner class to output a two digit month.

private static class
FastDatePrinter.TwoDigitNumberField

Inner class to output a two digit number.

private static class
FastDatePrinter.TwoDigitYearField

Inner class to output a two digit year.

private static class
FastDatePrinter.UnpaddedMonthField

Inner class to output an unpadded month.

private static class
FastDatePrinter.UnpaddedNumberField

Inner class to output an unpadded number.

private static class
FastDatePrinter.WeekYear

Inner class to output the numeric day in week.

Field Summary

Modifier and TypeField and Description
private static final FastDatePrinter.Rule[]
EMPTY_RULE_ARRAY

Empty array.

public static final int
FULL

FULL locale dependent date or time style.

private final Locale
locale

The locale.

public static final int
LONG

LONG locale dependent date or time style.

private static final int
private transient int
maxLengthEstimate

The estimated maximum length.

public static final int
MEDIUM

MEDIUM locale dependent date or time style.

private final String
pattern

The pattern.

private transient FastDatePrinter.Rule[]
rules

The parsed rules.

private static final long
serialVersionUID

Required for serialization support.

public static final int
SHORT

SHORT locale dependent date or time style.

private final TimeZone
timeZone

The time zone.

private static final ConcurrentMap<FastDatePrinter.TimeZoneDisplayKey, String>

Constructor Summary

AccessConstructor and Description
protected
FastDatePrinter(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 FastDatePrinter.

Method Summary

Modifier and TypeMethod and Description
private static void
appendDigits(final Appendable
the buffer to append to.
buffer
,
final int
the value to append digits from.
value
)

Appends two digits to the given buffer.

private static void
appendFullDigits(final Appendable
the buffer to append to.
buffer
,
int
the value to append digits from.
value
,
int
Minimum field width.
minFieldWidth
)

Appends all digits to the given buffer.

private <
the Appendable class type, usually StringBuilder or StringBuffer.
B extends Appendable
>
B

Returns:

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

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

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.

private String

Returns:

a String representation of the given Calendar.
applyRulesToString
(final Calendar
the Calendar to apply the rules to.
c
)

Creates a String representation of the given Calendar by applying the rules of this printer to it.

pack-priv static void
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
format(final Calendar calendar)

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

Formats a Calendar object.

public <B extends Appendable> B
format(final Calendar calendar, final B buf)

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

Formats a Calendar object into the supplied Appendable.

public StringBuffer
public String
format(final Date date)

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

Formats a Date object using a GregorianCalendar.

public <B extends Appendable> B
format(final Date date, final B buf)

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

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

public StringBuffer
public String
format(final long millis)

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

Formats a millisecond long value.

public <B extends Appendable> B
format(final long millis, final B buf)

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

Formats a millisecond long value into the supplied Appendable.

public StringBuffer
format(final long millis, final StringBuffer buf)

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

Formats a millisecond long value into the supplied StringBuffer.

pack-priv String

Returns:

The formatted value.
format
(final Object
the object to format.
obj
)

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

public StringBuffer

Returns:

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

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

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

public Locale
getLocale()

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

Gets the locale used by this printer.

public int

Returns:

the maximum formatted length.
getMaxLengthEstimate
()

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

public String
getPattern()

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

Gets the pattern used by this printer.

public TimeZone
getTimeZone()

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

Gets the time zone used by this printer.

pack-priv static String

Returns:

the textual name of the time zone.
getTimeZoneDisplay
(final TimeZone
the zone to query.
tz
,
final boolean
true if daylight savings.
daylight
,
final int
the style to use TimeZone#LONG or TimeZone#SHORT.
style
,
final Locale
the locale to use.
locale
)

Gets the time zone display name, using a cache for performance.

public int

Returns:

a hash code compatible with equals.
hashCode
()

Overrides java.lang.Object.hashCode.

Returns a hash code compatible with equals.

private void
init()

Initializes the instance for first use.

private Calendar

Returns:

a new Calendar instance.
newCalendar
()

Creates a new Calendar instance.

protected List<FastDatePrinter.Rule>

Returns:

a List of Rule objects.
parsePattern
()

Returns a list of Rules given a pattern.

protected String

Returns:

parsed token.
parseToken
(final String
the pattern.
pattern
,
final int[]
index references.
indexRef
)

Performs the parsing of tokens.

private void
readObject(final ObjectInputStream
ObjectInputStream from which the object is being deserialized.
in
)

Create the object after serialization.

protected FastDatePrinter.NumberRule

Returns:

a new rule with the correct padding.
selectNumberRule
(final int
the field to get a rule for.
field
,
final int
the padding required.
padding
)

Gets an appropriate rule for the padding required.

public String

Returns:

a debugging string.
toString
()

Overrides java.lang.Object.toString.

Gets a debugging string version of this formatter.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait