FastDateParser
- public class org.apache.commons.lang3.time.FastDateParser implements DateParser, Serializable[source]
FastDateParser is a fast and thread-safe version of
java.text.SimpleDateFormat.To obtain a proxy to a FastDateParser, use
FastDateFormat#getInstance(String, TimeZone, Locale)or another variation of the factory methods ofFastDateFormat.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
SimpleDateFormatin most parsing situations. This class is especially useful in multi-threaded server environments.SimpleDateFormatis 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:
Constructors
Constructs a new FastDateParser. |
|
Constructs a new FastDateParser. |
- protected FastDateParser(String pattern, TimeZone timeZone, Locale locale)[source]
Constructs a new FastDateParser. Use
FastDateFormat#getInstance(String, TimeZone, Locale)or another variation of the factory methods ofFastDateFormatto get a cached FastDateParser instance.- Parameters:
pattern – non-null
java.text.SimpleDateFormatcompatible patterntimeZone – non-null time zone to use
locale – non-null locale
- protected FastDateParser(String pattern, TimeZone timeZone, Locale locale, Date centuryStart)[source]
Constructs a new FastDateParser.
- Parameters:
pattern – non-null
java.text.SimpleDateFormatcompatible patterntimeZone – non-null time zone to use
locale – locale, null maps to the default Locale.
centuryStart – The start of the century for 2 digit year parsing
- Since:
3.5
Methods
Compares another object for equality with this object. |
|
Gets the locale used by this parser. |
|
Gets the pattern used by this parser. |
|
Gets the time zone used by this parser. |
|
Returns a hash code compatible with equals. |
|
Equivalent to DateFormat.parse(String). |
|
This implementation updates the ParsePosition if the parse succeeds. |
|
Parses a formatted date string according to the format. |
|
Parses text from a string to produce a Date. |
|
Parses a date/time string according to the given parse position. |
|
Gets a string version of this formatter. |
|
Converts all state of this instance to a String handy for debugging. |
- @Override public boolean equals(Object obj)[source]
Compares another object for equality with this object.
- Parameters:
obj – The object to compare to
- Returns:
trueif equal to this instance
- @Override public Locale getLocale()[source]
Gets the locale used by this parser.
- Returns:
The locale
- @Override public String getPattern()[source]
Gets the pattern used by this parser.
- Returns:
The pattern,
java.text.SimpleDateFormatcompatible.
- @Override public TimeZone getTimeZone()[source]
Gets the time zone used by this parser.
The default
TimeZoneused to create aDatewhen theTimeZoneis not specified by the format pattern.- Returns:
The time zone
- @Override public int hashCode()[source]
Returns a hash code compatible with equals.
- Returns:
A hash code compatible with equals
- @Override public Date parse(String source) throws ParseException[source]
Equivalent to DateFormat.parse(String). See
java.text.DateFormat#parse(String)for more information.
- @Override public Date parse(String source, ParsePosition pos)[source]
This implementation updates the ParsePosition if the parse succeeds. However, it sets the error index to the position before the failed field unlike the method
java.text.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.- See also:
org.apache.commons.lang3.time.DateParser#parse(String, java.text.ParsePosition)- Parameters:
source – A
String, part of which should be parsed.pos – A
ParsePositionobject with index and error index information as described above.
- Returns:
A
Dateparsed from the string. In case of error, returns null.
- @Override public boolean parse(String source, ParsePosition pos, Calendar calendar)[source]
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.
- 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:
java.lang.IllegalArgumentException – Thrown when Calendar has been set to be not lenient, and a parsed field is out of range.
- @Override public Object parseObject(String source) throws ParseException[source]
Parses text from a string to produce a Date.
- Parameters:
source – A
Stringwhose beginning should be parsed.- Returns:
A
java.util.Dateobject.- Throws:
java.text.ParseException – Thrown if the beginning of the specified string cannot be parsed.
- @Override public Object parseObject(String source, ParsePosition pos)[source]
Parses a date/time string according to the given parse position.
- Parameters:
source – A
Stringwhose beginning should be parsed.pos – The parse position.
- Returns:
A
java.util.Dateobject.