Class FastDateParser
- All Implemented Interfaces:
Serializable
,DateParser
FastDateParser is a fast and thread-safe version of
SimpleDateFormat
.
To obtain a proxy to a FastDateParser, use FastDateFormat.getInstance(String, TimeZone, Locale)
or another variation of the factory methods of FastDateFormat
.
Since FastDateParser is thread safe, you can use a static member instance:
private static final DateParser DATE_PARSER = FastDateFormat.getInstance("yyyy-MM-dd");
This class can be used as a direct replacement for
SimpleDateFormat
in most 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 has closed the
bug/RFE.
Only parsing is supported by this class, but all patterns are compatible with SimpleDateFormat.
The class operates in lenient mode, so for example a time of 90 minutes is treated as 1 hour 30 minutes.
Timing tests indicate this class is as about as fast as SimpleDateFormat in single thread applications and about 25% faster in multi-thread applications.
- Since:
- 3.2
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
A strategy that handles a text field in the parsing patternprivate static class
A strategy that copies the static or quoted field in the parsing patternprivate static class
private static class
A strategy that handles a number field in the parsing patternprivate static class
A strategy to parse a single field from the parsing patternprivate static class
A strategy to parse a single field from the parsing patternprivate static class
Holds strategy and field widthprivate class
Parse format into Strategies(package private) static class
A strategy that handles a time zone field in the parsing pattern -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final FastDateParser.Strategy
private static final ConcurrentMap<Locale,
FastDateParser.Strategy>[] private final int
private static final FastDateParser.Strategy
private static final FastDateParser.Strategy
private static final FastDateParser.Strategy
private static final FastDateParser.Strategy
private static final FastDateParser.Strategy
private static final FastDateParser.Strategy
private static final FastDateParser.Strategy
private static final FastDateParser.Strategy
(package private) static final Locale
private static final FastDateParser.Strategy
private final Locale
private static final Comparator<String>
private static final FastDateParser.Strategy
private static final FastDateParser.Strategy
private static final FastDateParser.Strategy
private final String
private List<FastDateParser.StrategyAndWidth>
private static final FastDateParser.Strategy
private static final long
Required for serialization support.private final int
private final TimeZone
private static final FastDateParser.Strategy
private static final FastDateParser.Strategy
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
FastDateParser
(String pattern, TimeZone timeZone, Locale locale) Constructs a new FastDateParser.protected
FastDateParser
(String pattern, TimeZone timeZone, Locale locale, Date centuryStart) Constructs a new FastDateParser. -
Method Summary
Modifier and TypeMethodDescriptionprivate int
adjustYear
(int twoDigitYear) Adjusts dates to be within appropriate centuryappendDisplayNames
(Calendar calendar, Locale locale, int field, StringBuilder regex) Gets the short and long values displayed for a fieldboolean
Compares another object for equality with this object.private static ConcurrentMap<Locale,
FastDateParser.Strategy> getCache
(int field) Gets a cache of Strategies for a particular fieldGets the locale used by this parser.private FastDateParser.Strategy
getLocaleSpecificStrategy
(int field, Calendar definingCalendar) Constructs a Strategy that parses a Text fieldGets the pattern used by this parser.private FastDateParser.Strategy
getStrategy
(char f, int width, Calendar definingCalendar) Gets a Strategy given a field from a SimpleDateFormat patternGets the time zone used by this parser.int
hashCode()
Returns a hash code compatible with equals.private void
Initializes derived fields from defining fields.private static boolean
isFormatLetter
(char c) Equivalent to DateFormat.parse(String).parse
(String source, ParsePosition pos) This implementation updates the ParsePosition if the parse succeeds.boolean
parse
(String source, ParsePosition pos, Calendar calendar) Parses a formatted date string according to the format.parseObject
(String source) Parses text from a string to produce a Date.parseObject
(String source, ParsePosition pos) Parses a date/time string according to the given parse position.private void
Creates the object after serialization.private static StringBuilder
simpleQuote
(StringBuilder sb, String value) toString()
Gets a string version of this formatter.Converts all state of this instance to a String handy for debugging.
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDRequired for serialization support.- See Also:
-
JAPANESE_IMPERIAL
-
pattern
-
timeZone
-
locale
-
century
private final int century -
startYear
private final int startYear -
patterns
-
LONGER_FIRST_LOWERCASE
-
caches
-
ABBREVIATED_YEAR_STRATEGY
-
NUMBER_MONTH_STRATEGY
-
LITERAL_YEAR_STRATEGY
-
WEEK_OF_YEAR_STRATEGY
-
WEEK_OF_MONTH_STRATEGY
-
DAY_OF_YEAR_STRATEGY
-
DAY_OF_MONTH_STRATEGY
-
DAY_OF_WEEK_STRATEGY
-
DAY_OF_WEEK_IN_MONTH_STRATEGY
-
HOUR_OF_DAY_STRATEGY
-
HOUR24_OF_DAY_STRATEGY
-
HOUR12_STRATEGY
-
HOUR_STRATEGY
-
MINUTE_STRATEGY
-
SECOND_STRATEGY
-
MILLISECOND_STRATEGY
-
-
Constructor Details
-
FastDateParser
Constructs a new FastDateParser.
UseFastDateFormat.getInstance(String, TimeZone, Locale)
or another variation of the factory methods ofFastDateFormat
to get a cached FastDateParser instance.- Parameters:
pattern
- non-nullSimpleDateFormat
compatible patterntimeZone
- non-null time zone to uselocale
- non-null locale
-
FastDateParser
Constructs a new FastDateParser.
- Parameters:
pattern
- non-nullSimpleDateFormat
compatible patterntimeZone
- non-null time zone to uselocale
- non-null localecenturyStart
- The start of the century for 2 digit year parsing- Since:
- 3.5
-
-
Method Details
-
init
Initializes derived fields from defining fields. This is called from constructor and from readObject (de-serialization)- Parameters:
definingCalendar
- theCalendar
instance used to initialize this FastDateParser
-
isFormatLetter
private static boolean isFormatLetter(char c) -
getPattern
Description copied from interface:DateParser
Gets the pattern used by this parser.
- Specified by:
getPattern
in interfaceDateParser
- Returns:
- the pattern,
SimpleDateFormat
compatible
-
getTimeZone
Description copied from interface:DateParser
Gets the time zone used by this parser.
The default
TimeZone
used to create aDate
when theTimeZone
is not specified by the format pattern.- Specified by:
getTimeZone
in interfaceDateParser
- Returns:
- the time zone
-
getLocale
Description copied from interface:DateParser
Gets the locale used by this parser.
- Specified by:
getLocale
in interfaceDateParser
- Returns:
- the locale
-
equals
Compares another object for equality with this object.
-
hashCode
public int hashCode()Returns a hash code compatible with equals.
-
toString
Gets a string version of this formatter.
-
toStringAll
Converts all state of this instance to a String handy for debugging.- Returns:
- a string.
- Since:
- 3.12.0
-
readObject
Creates the object after serialization. This implementation reinitializes the transient properties.- Parameters:
in
- ObjectInputStream from which the object is being deserialized.- Throws:
IOException
- if there is an IO issue.ClassNotFoundException
- if a class cannot be found.
-
parseObject
Description copied from interface:DateParser
Parses text from a string to produce a Date.- Specified by:
parseObject
in interfaceDateParser
- Parameters:
source
- AString
whose beginning should be parsed.- Returns:
- a
java.util.Date
object - Throws:
ParseException
- if the beginning of the specified string cannot be parsed.- See Also:
-
parse
Description copied from interface:DateParser
Equivalent to DateFormat.parse(String). SeeDateFormat.parse(String)
for more information.- Specified by:
parse
in interfaceDateParser
- Parameters:
source
- AString
whose beginning should be parsed.- Returns:
- A
Date
parsed from the string - Throws:
ParseException
- if the beginning of the specified string cannot be parsed.
-
parseObject
Description copied from interface:DateParser
Parses a date/time string according to the given parse position.- Specified by:
parseObject
in interfaceDateParser
- Parameters:
source
- AString
whose beginning should be parsed.pos
- the parse position- Returns:
- a
java.util.Date
object - See Also:
-
parse
This implementation updates the ParsePosition if the parse succeeds. However, it sets the error index to the position before the failed field unlike the methodSimpleDateFormat.parse(String, ParsePosition)
which sets the error index to after the failed field.To determine if the parse has succeeded, the caller must check if the current parse position given by
ParsePosition.getIndex()
has been updated. If the input buffer has been fully parsed, then the index will point to just after the end of the input buffer.- Specified by:
parse
in interfaceDateParser
- Parameters:
source
- AString
, part of which should be parsed.pos
- AParsePosition
object with index and error index information as described above.- Returns:
- A
Date
parsed from the string. In case of error, returns null. - See Also:
-
parse
Parses a formatted date string according to the format. Updates the Calendar with parsed fields. Upon success, the ParsePosition index is updated to indicate how much of the source text was consumed. Not all source text needs to be consumed. Upon parse failure, ParsePosition error index is updated to the offset of the source text which does not match the supplied format.- Specified by:
parse
in interfaceDateParser
- Parameters:
source
- The text to parse.pos
- On input, the position in the source to start parsing, on output, updated position.calendar
- The calendar into which to set parsed fields.- Returns:
- true, if source has been parsed (pos parsePosition is updated); otherwise false (and pos errorIndex is updated)
- Throws:
IllegalArgumentException
- when Calendar has been set to be not lenient, and a parsed field is out of range.
-
simpleQuote
-
appendDisplayNames
private static Map<String,Integer> appendDisplayNames(Calendar calendar, Locale locale, int field, StringBuilder regex) Gets the short and long values displayed for a field- Parameters:
calendar
- The calendar to obtain the short and long valueslocale
- The locale of display namesfield
- The field of interestregex
- The regular expression to build- Returns:
- The map of string display names to field values
-
adjustYear
private int adjustYear(int twoDigitYear) Adjusts dates to be within appropriate century- Parameters:
twoDigitYear
- The year to adjust- Returns:
- A value between centuryStart(inclusive) to centuryStart+100(exclusive)
-
getStrategy
Gets a Strategy given a field from a SimpleDateFormat pattern- Parameters:
f
- A sub-sequence of the SimpleDateFormat patterndefiningCalendar
- The calendar to obtain the short and long values- Returns:
- The Strategy that will handle parsing for the field
-
getCache
Gets a cache of Strategies for a particular field- Parameters:
field
- The Calendar field- Returns:
- a cache of Locale to Strategy
-
getLocaleSpecificStrategy
Constructs a Strategy that parses a Text field- Parameters:
field
- The Calendar fielddefiningCalendar
- The calendar to obtain the short and long values- Returns:
- a TextStrategy for the field and Locale
-