Class FastDateParser
- All Implemented Interfaces:
Serializable,DateParser
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
- Version:
- $Id$
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classA strategy that handles a text field in the parsing patternprivate static classA strategy that copies the static or quoted field in the parsing patternprivate static classprivate static classA strategy that handles a number field in the parsing patternprivate static classA strategy to parse a single field from the parsing patternprivate static classA strategy that handles a timezone field in the parsing pattern -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final FastDateParser.Strategyprivate static final ConcurrentMap<Locale,FastDateParser.Strategy>[] private final intprivate Stringprivate static final FastDateParser.Strategyprivate static final FastDateParser.Strategyprivate static final FastDateParser.Strategyprivate static final PatternAPatternto parse the user supplied SimpleDateFormat patternprivate static final FastDateParser.Strategyprivate static final FastDateParser.Strategyprivate static final FastDateParser.Strategyprivate static final FastDateParser.Strategyprivate static final FastDateParser.Strategy(package private) static final Localeprivate static final FastDateParser.Strategyprivate final Localeprivate static final FastDateParser.Strategyprivate static final FastDateParser.Strategyprivate FastDateParser.Strategyprivate static final FastDateParser.Strategyprivate Patternprivate final Stringprivate static final FastDateParser.Strategyprivate static final longRequired for serialization support.private final intprivate FastDateParser.Strategy[]private final TimeZoneprivate static final FastDateParser.Strategyprivate static final FastDateParser.Strategy -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedFastDateParser(String pattern, TimeZone timeZone, Locale locale) Constructs a new FastDateParser.protectedFastDateParser(String pattern, TimeZone timeZone, Locale locale, Date centuryStart) Constructs a new FastDateParser. -
Method Summary
Modifier and TypeMethodDescriptionprivate intadjustYear(int twoDigitYear) Adjust dates to be within appropriate centurybooleanCompare another object for equality with this object.private static StringBuilderescapeRegex(StringBuilder regex, String value, boolean unquote) Escape constant fields into regular expressionprivate static ConcurrentMap<Locale, FastDateParser.Strategy> getCache(int field) Get a cache of Strategies for a particular fieldgetDisplayNames(int field, Calendar definingCalendar, Locale locale) Get the short and long values displayed for a field(package private) intWhat is the width of the current field?Get the locale used by this parser.private FastDateParser.StrategygetLocaleSpecificStrategy(int field, Calendar definingCalendar) Construct a Strategy that parses a Text field(package private) PatternReturns the generated pattern (for testing purposes).Get the pattern used by this parser.private FastDateParser.StrategygetStrategy(String formatField, Calendar definingCalendar) Obtain a Strategy given a field from a SimpleDateFormat patternGet the time zone used by this parser.inthashCode()Return a hashcode compatible with equals.private voidInitialize derived fields from defining fields.(package private) booleanIs the next field a number?Equivalent to DateFormat.parse(String).parse(String source, ParsePosition pos) This implementation updates the ParsePosition if the parse succeeds.parseObject(String source) Parses text from a string to produce a Date.parseObject(String source, ParsePosition pos) Parse a date/time string according to the given parse position.private voidCreate the object after serialization.toString()Get a string version of this formatter.
-
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 -
parsePattern
-
strategies
-
currentFormatField
-
nextStrategy
-
formatPattern
APatternto parse the user supplied SimpleDateFormat pattern -
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_IN_MONTH_STRATEGY
-
HOUR_OF_DAY_STRATEGY
-
HOUR24_OF_DAY_STRATEGY
-
HOUR12_STRATEGY
-
HOUR_STRATEGY
-
MINUTE_STRATEGY
-
SECOND_STRATEGY
-
MILLISECOND_STRATEGY
-
ISO_8601_STRATEGY
-
-
Constructor Details
-
FastDateParser
Constructs a new FastDateParser. UseFastDateFormat.getInstance(String, TimeZone, Locale)or another variation of the factory methods ofFastDateFormatto get a cached FastDateParser instance.- Parameters:
pattern- non-nullSimpleDateFormatcompatible patterntimeZone- non-null time zone to uselocale- non-null locale
-
FastDateParser
Constructs a new FastDateParser.- Parameters:
pattern- non-nullSimpleDateFormatcompatible patterntimeZone- non-null time zone to uselocale- non-null localecenturyStart- The start of the century for 2 digit year parsing- Since:
- 3.3
-
-
Method Details
-
init
Initialize derived fields from defining fields. This is called from constructor and from readObject (de-serialization)- Parameters:
definingCalendar- theCalendarinstance used to initialize this FastDateParser
-
getPattern
Description copied from interface:DateParserGet the pattern used by this parser.- Specified by:
getPatternin interfaceDateParser- Returns:
- the pattern,
SimpleDateFormatcompatible
-
getTimeZone
Description copied from interface:DateParserGet the time zone used by this parser.The default
TimeZoneused to create aDatewhen theTimeZoneis not specified by the format pattern.- Specified by:
getTimeZonein interfaceDateParser- Returns:
- the time zone
-
getLocale
Description copied from interface:DateParserGet the locale used by this parser.- Specified by:
getLocalein interfaceDateParser- Returns:
- the locale
-
getParsePattern
Pattern getParsePattern()Returns the generated pattern (for testing purposes).- Returns:
- the generated pattern
-
equals
Compare another object for equality with this object. -
hashCode
public int hashCode()Return a hashcode compatible with equals. -
toString
Get a string version of this formatter. -
readObject
Create 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:DateParserParses text from a string to produce a Date.- Specified by:
parseObjectin interfaceDateParser- Parameters:
source- AStringwhose beginning should be parsed.- Returns:
- a
java.util.Dateobject - Throws:
ParseException- if the beginning of the specified string cannot be parsed.- See Also:
-
parse
Description copied from interface:DateParserEquivalent to DateFormat.parse(String).See
DateFormat.parse(String)for more information.- Specified by:
parsein interfaceDateParser- Parameters:
source- AStringwhose beginning should be parsed.- Returns:
- A
Dateparsed from the string - Throws:
ParseException- if the beginning of the specified string cannot be parsed.
-
parseObject
Description copied from interface:DateParserParse a date/time string according to the given parse position.- Specified by:
parseObjectin interfaceDateParser- Parameters:
source- AStringwhose beginning should be parsed.pos- the parse position- Returns:
- a
java.util.Dateobject - See Also:
-
parse
This implementation updates the ParsePosition if the parse succeeds. However, unlike the methodSimpleDateFormat.parse(String, ParsePosition)it is not able to set the error Index - i.e.ParsePosition.getErrorIndex()- if the parse fails.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.See org.apache.commons.lang3.time.DateParser#parse(java.lang.String, java.text.ParsePosition) Equivalent to DateFormat.parse(String, ParsePosition).
See
DateFormat.parse(String, ParsePosition)for more information.- Specified by:
parsein interfaceDateParser- Parameters:
source- AString, part of which should be parsed.pos- AParsePositionobject with index and error index information as described above.- Returns:
- A
Dateparsed from the string. In case of error, returns null.
-
escapeRegex
Escape constant fields into regular expression- Parameters:
regex- The destination regexvalue- The source fieldunquote- If true, replace two success quotes ('') with single quote (')- Returns:
- The
StringBuilder
-
getDisplayNames
private static Map<String,Integer> getDisplayNames(int field, Calendar definingCalendar, Locale locale) Get the short and long values displayed for a field- Parameters:
field- The field of interestdefiningCalendar- The calendar to obtain the short and long valueslocale- The locale of display names- Returns:
- A Map of the field key / value pairs
-
adjustYear
private int adjustYear(int twoDigitYear) Adjust dates to be within appropriate century- Parameters:
twoDigitYear- The year to adjust- Returns:
- A value between centuryStart(inclusive) to centuryStart+100(exclusive)
-
isNextNumber
boolean isNextNumber()Is the next field a number?- Returns:
- true, if next field will be a number
-
getFieldWidth
int getFieldWidth()What is the width of the current field?- Returns:
- The number of characters in the current format field
-
getStrategy
Obtain a Strategy given a field from a SimpleDateFormat pattern- Parameters:
formatField- 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
Get a cache of Strategies for a particular field- Parameters:
field- The Calendar field- Returns:
- a cache of Locale to Strategy
-
getLocaleSpecificStrategy
Construct 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
-