public class FastDateParser extends java.lang.Object implements DateParser, java.io.Serializable
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.
FastDatePrinter
,
Serialized FormModifier and Type | Class and Description |
---|---|
private static class |
FastDateParser.CaseInsensitiveTextStrategy
A strategy that handles a text field in the parsing pattern
|
private static class |
FastDateParser.CopyQuotedStrategy
A strategy that copies the static or quoted field in the parsing pattern
|
private static class |
FastDateParser.ISO8601TimeZoneStrategy |
private static class |
FastDateParser.NumberStrategy
A strategy that handles a number field in the parsing pattern
|
private static class |
FastDateParser.PatternStrategy
A strategy to parse a single field from the parsing pattern
|
private static class |
FastDateParser.Strategy
A strategy to parse a single field from the parsing pattern
|
private static class |
FastDateParser.StrategyAndWidth
Holds strategy and field width
|
private class |
FastDateParser.StrategyParser
Parse format into Strategies
|
(package private) static class |
FastDateParser.TimeZoneStrategy
A strategy that handles a timezone field in the parsing pattern
|
Modifier | Constructor and Description |
---|---|
protected |
FastDateParser(java.lang.String pattern,
java.util.TimeZone timeZone,
java.util.Locale locale)
Constructs a new FastDateParser.
|
protected |
FastDateParser(java.lang.String pattern,
java.util.TimeZone timeZone,
java.util.Locale locale,
java.util.Date centuryStart)
Constructs a new FastDateParser.
|
Modifier and Type | Method and Description |
---|---|
private int |
adjustYear(int twoDigitYear)
Adjust dates to be within appropriate century
|
private static java.util.Map<java.lang.String,java.lang.Integer> |
appendDisplayNames(java.util.Calendar cal,
java.util.Locale locale,
int field,
java.lang.StringBuilder regex)
Get the short and long values displayed for a field
|
boolean |
equals(java.lang.Object obj)
Compare another object for equality with this object.
|
private static java.util.concurrent.ConcurrentMap<java.util.Locale,FastDateParser.Strategy> |
getCache(int field)
Get a cache of Strategies for a particular field
|
java.util.Locale |
getLocale()
Gets the locale used by this parser.
|
private FastDateParser.Strategy |
getLocaleSpecificStrategy(int field,
java.util.Calendar definingCalendar)
Construct a Strategy that parses a Text field
|
java.lang.String |
getPattern()
Gets the pattern used by this parser.
|
private FastDateParser.Strategy |
getStrategy(char f,
int width,
java.util.Calendar definingCalendar)
Obtain a Strategy given a field from a SimpleDateFormat pattern
|
java.util.TimeZone |
getTimeZone()
Gets the time zone used by this parser.
|
int |
hashCode()
Return a hash code compatible with equals.
|
private void |
init(java.util.Calendar definingCalendar)
Initialize derived fields from defining fields.
|
private static boolean |
isFormatLetter(char c) |
java.util.Date |
parse(java.lang.String source)
Equivalent to DateFormat.parse(String).
|
java.util.Date |
parse(java.lang.String source,
java.text.ParsePosition pos)
This implementation updates the ParsePosition if the parse succeeds.
|
boolean |
parse(java.lang.String source,
java.text.ParsePosition pos,
java.util.Calendar calendar)
Parse a formatted date string according to the format.
|
java.lang.Object |
parseObject(java.lang.String source)
Parses text from a string to produce a Date.
|
java.lang.Object |
parseObject(java.lang.String source,
java.text.ParsePosition pos)
Parses a date/time string according to the given parse position.
|
private void |
readObject(java.io.ObjectInputStream in)
Create the object after serialization.
|
private static java.lang.StringBuilder |
simpleQuote(java.lang.StringBuilder sb,
java.lang.String value) |
java.lang.String |
toString()
Get a string version of this formatter.
|
private static final long serialVersionUID
Serializable
,
Constant Field Valuesstatic final java.util.Locale JAPANESE_IMPERIAL
private final java.lang.String pattern
private final java.util.TimeZone timeZone
private final java.util.Locale locale
private final int century
private final int startYear
private transient java.util.List<FastDateParser.StrategyAndWidth> patterns
private static final java.util.Comparator<java.lang.String> LONGER_FIRST_LOWERCASE
private static final java.util.concurrent.ConcurrentMap<java.util.Locale,FastDateParser.Strategy>[] caches
private static final FastDateParser.Strategy ABBREVIATED_YEAR_STRATEGY
private static final FastDateParser.Strategy NUMBER_MONTH_STRATEGY
private static final FastDateParser.Strategy LITERAL_YEAR_STRATEGY
private static final FastDateParser.Strategy WEEK_OF_YEAR_STRATEGY
private static final FastDateParser.Strategy WEEK_OF_MONTH_STRATEGY
private static final FastDateParser.Strategy DAY_OF_YEAR_STRATEGY
private static final FastDateParser.Strategy DAY_OF_MONTH_STRATEGY
private static final FastDateParser.Strategy DAY_OF_WEEK_STRATEGY
private static final FastDateParser.Strategy DAY_OF_WEEK_IN_MONTH_STRATEGY
private static final FastDateParser.Strategy HOUR_OF_DAY_STRATEGY
private static final FastDateParser.Strategy HOUR24_OF_DAY_STRATEGY
private static final FastDateParser.Strategy HOUR12_STRATEGY
private static final FastDateParser.Strategy HOUR_STRATEGY
private static final FastDateParser.Strategy MINUTE_STRATEGY
private static final FastDateParser.Strategy SECOND_STRATEGY
private static final FastDateParser.Strategy MILLISECOND_STRATEGY
protected FastDateParser(java.lang.String pattern, java.util.TimeZone timeZone, java.util.Locale locale)
Constructs a new FastDateParser.
UseFastDateFormat.getInstance(String, TimeZone, Locale)
or another variation of the
factory methods of FastDateFormat
to get a cached FastDateParser instance.pattern
- non-null SimpleDateFormat
compatible
patterntimeZone
- non-null time zone to uselocale
- non-null localeprotected FastDateParser(java.lang.String pattern, java.util.TimeZone timeZone, java.util.Locale locale, java.util.Date centuryStart)
Constructs a new FastDateParser.
pattern
- non-null SimpleDateFormat
compatible
patterntimeZone
- non-null time zone to uselocale
- non-null localecenturyStart
- The start of the century for 2 digit year parsingprivate void init(java.util.Calendar definingCalendar)
definingCalendar
- the Calendar
instance used to initialize this FastDateParserprivate static boolean isFormatLetter(char c)
public java.lang.String getPattern()
DateParser
Gets the pattern used by this parser.
getPattern
in interface DateParser
SimpleDateFormat
compatiblepublic java.util.TimeZone getTimeZone()
DateParser
Gets the time zone used by this parser.
The default TimeZone
used to create a Date
when the TimeZone
is not specified by
the format pattern.
getTimeZone
in interface DateParser
public java.util.Locale getLocale()
DateParser
Gets the locale used by this parser.
getLocale
in interface DateParser
public boolean equals(java.lang.Object obj)
Compare another object for equality with this object.
equals
in class java.lang.Object
obj
- the object to compare totrue
if equal to this instancepublic int hashCode()
Return a hash code compatible with equals.
hashCode
in class java.lang.Object
public java.lang.String toString()
Get a string version of this formatter.
toString
in class java.lang.Object
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
in
- ObjectInputStream from which the object is being deserialized.java.io.IOException
- if there is an IO issue.java.lang.ClassNotFoundException
- if a class cannot be found.public java.lang.Object parseObject(java.lang.String source) throws java.text.ParseException
DateParser
parseObject
in interface DateParser
source
- A String
whose beginning should be parsed.java.util.Date
objectjava.text.ParseException
- if the beginning of the specified string cannot be parsed.Format.parseObject(String)
public java.util.Date parse(java.lang.String source) throws java.text.ParseException
DateParser
DateFormat.parse(String)
for more information.parse
in interface DateParser
source
- A String
whose beginning should be parsed.Date
parsed from the stringjava.text.ParseException
- if the beginning of the specified string cannot be parsed.public java.lang.Object parseObject(java.lang.String source, java.text.ParsePosition pos)
DateParser
parseObject
in interface DateParser
source
- A String
whose beginning should be parsed.pos
- the parse positionjava.util.Date
objectDateFormat.parseObject(String, ParsePosition)
public java.util.Date parse(java.lang.String source, java.text.ParsePosition pos)
SimpleDateFormat.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.
parse
in interface DateParser
source
- A String
, part of which should be parsed.pos
- A ParsePosition
object with index and error index information
as described above.Date
parsed from the string. In case of error, returns null.DateParser.parse(java.lang.String, java.text.ParsePosition)
public boolean parse(java.lang.String source, java.text.ParsePosition pos, java.util.Calendar calendar)
parse
in interface DateParser
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.java.lang.IllegalArgumentException
- when Calendar has been set to be not lenient, and a parsed field is
out of range.private static java.lang.StringBuilder simpleQuote(java.lang.StringBuilder sb, java.lang.String value)
private static java.util.Map<java.lang.String,java.lang.Integer> appendDisplayNames(java.util.Calendar cal, java.util.Locale locale, int field, java.lang.StringBuilder regex)
cal
- The calendar to obtain the short and long valueslocale
- The locale of display namesfield
- The field of interestregex
- The regular expression to buildprivate int adjustYear(int twoDigitYear)
twoDigitYear
- The year to adjustprivate FastDateParser.Strategy getStrategy(char f, int width, java.util.Calendar definingCalendar)
f
- A sub-sequence of the SimpleDateFormat patterndefiningCalendar
- The calendar to obtain the short and long valuesprivate static java.util.concurrent.ConcurrentMap<java.util.Locale,FastDateParser.Strategy> getCache(int field)
field
- The Calendar fieldprivate FastDateParser.Strategy getLocaleSpecificStrategy(int field, java.util.Calendar definingCalendar)
field
- The Calendar fielddefiningCalendar
- The calendar to obtain the short and long values