Class FastDateParser
- java.lang.Object
-
- org.sqlite.date.FastDateParser
-
- All Implemented Interfaces:
Serializable,DateParser
public class FastDateParser extends Object implements DateParser, Serializable
FastDateParser is a fast and thread-safe version ofSimpleDateFormat.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
- Version:
- $Id$
- See Also:
FastDatePrinter, Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedFastDateParser(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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object obj)Compare another object for equality with this object.LocalegetLocale()Get the locale used by this parser.StringgetPattern()Get the pattern used by this parser.TimeZonegetTimeZone()Get the time zone used by this parser.inthashCode()Return a hashcode compatible with equals.Dateparse(String source)Equivalent to DateFormat.parse(String).Dateparse(String source, ParsePosition pos)This implementation updates the ParsePosition if the parse succeeeds.ObjectparseObject(String source)Parses text from a string to produce a Date.ObjectparseObject(String source, ParsePosition pos)Parse a date/time string according to the given parse position.StringtoString()Get a string version of this formatter.
-
-
-
Constructor Detail
-
FastDateParser
protected FastDateParser(String pattern, TimeZone timeZone, Locale locale)
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
protected FastDateParser(String pattern, TimeZone timeZone, Locale locale, Date centuryStart)
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 Detail
-
getPattern
public String getPattern()
Description copied from interface:DateParserGet the pattern used by this parser.- Specified by:
getPatternin interfaceDateParser- Returns:
- the pattern,
SimpleDateFormatcompatible
-
getTimeZone
public TimeZone 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
public Locale getLocale()
Description copied from interface:DateParserGet the locale used by this parser.- Specified by:
getLocalein interfaceDateParser- Returns:
- the locale
-
equals
public boolean equals(Object obj)
Compare another object for equality with this object.
-
hashCode
public int hashCode()
Return a hashcode compatible with equals.
-
toString
public String toString()
Get a string version of this formatter.
-
parseObject
public Object parseObject(String source) throws ParseException
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:
Format.parseObject(String)
-
parse
public Date parse(String source) throws ParseException
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
public Object parseObject(String source, ParsePosition pos)
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:
DateFormat.parseObject(String, ParsePosition)
-
parse
public Date parse(String source, ParsePosition pos)
This implementation updates the ParsePosition if the parse succeeeds. 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.
-
-