Top Description Inners Fields Constructors Methods
org.apache.commons.lang3.time

public Class FastDateParser

extends Object
implements DateParser, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.apache.commons.lang3.time.DateParser
Imports
java.io.IOException, .ObjectInputStream, .Serializable, java.text.DateFormatSymbols, .ParseException, .ParsePosition, .SimpleDateFormat, java.util.ArrayList, .Arrays, .Calendar, .Comparator, .Date, .HashMap, .List, .ListIterator, .Locale, .Map, .Objects, .Set, .TimeZone, .TreeMap, .TreeSet, java.util.concurrent.ConcurrentHashMap, .ConcurrentMap, java.util.regex.Matcher, .Pattern, java.util.stream.Stream, org.apache.commons.lang3.ArraySorter, .CharUtils, .LocaleUtils

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 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
FastDatePrinter

Nested and Inner Type Summary

Modifier and TypeClass 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
private static class
FastDateParser.NumberStrategy

A strategy that handles a number field in the parsing pattern

private abstract static class
FastDateParser.PatternStrategy

A strategy to parse a single field from the parsing pattern

private abstract 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

pack-priv static class
FastDateParser.TimeZoneStrategy

A strategy that handles a time zone field in the parsing pattern

Field Summary

Modifier and TypeField and Description
private static final FastDateParser.Strategy
private static final ConcurrentMap<Locale, FastDateParser.Strategy>[]
private final int
century

Century from Date.

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
pack-priv static final Locale
private static final FastDateParser.Strategy
private final Locale
locale

Input Locale.

private static final Comparator<String>
LONGER_FIRST_LOWERCASE

comparator used to sort regex alternatives.

private static final FastDateParser.Strategy
private static final FastDateParser.Strategy
private static final FastDateParser.Strategy
private final String
pattern

Input pattern.

private transient List<FastDateParser.StrategyAndWidth>
patterns

Initialized from Calendar.

private static final FastDateParser.Strategy
private static final long
serialVersionUID

Required for serialization support.

private final int
startYear

Start year from Date.

private final TimeZone
timeZone

Input TimeZone.

private static final FastDateParser.Strategy
private static final FastDateParser.Strategy

Constructor Summary

AccessConstructor and Description
protected
FastDateParser(final String
non-null java.text.SimpleDateFormat compatible pattern
pattern
,
final TimeZone
non-null time zone to use
timeZone
,
final Locale
non-null locale
locale
)

Constructs a new FastDateParser.

protected
FastDateParser(final String
non-null java.text.SimpleDateFormat compatible pattern
pattern
,
final TimeZone
non-null time zone to use
timeZone
,
final Locale
locale, null maps to the default Locale.
locale
,
final Date
The start of the century for 2 digit year parsing
centuryStart
)

Constructs a new FastDateParser.

Method Summary

Modifier and TypeMethod and Description
private int

Returns:

A value between centuryStart(inclusive) to centuryStart+100(exclusive)
adjustYear
(final int
The year to adjust
twoDigitYear
)

Adjusts dates to be within appropriate century

private static Map<String, Integer>

Returns:

The map of string display names to field values
appendDisplayNames
(final Calendar
The calendar to obtain the short and long values
calendar
,
final Locale
The locale of display names
locale
,
final int
The field of interest
field
,
final StringBuilder
The regular expression to build
regex
)

Gets the short and long values displayed for a field

pack-priv static void
clear()

Clears the cache.

public boolean

Returns:

trueif equal to this instance
equals
(final Object
the object to compare to
obj
)

Overrides java.lang.Object.equals.

Compares another object for equality with this object.

private static ConcurrentMap<Locale, FastDateParser.Strategy>

Returns:

a cache of Locale to Strategy
getCache
(final int
The Calendar field
field
)

Gets a cache of Strategies for a particular field

public Locale
getLocale()

Implements org.apache.commons.lang3.time.DateParser.getLocale.

Gets the locale used by this parser.

private FastDateParser.Strategy

Returns:

a TextStrategy for the field and Locale
getLocaleSpecificStrategy
(final int
The Calendar field
field
,
final Calendar
The calendar to obtain the short and long values
definingCalendar
)

Constructs a Strategy that parses a Text field

public String
getPattern()

Implements org.apache.commons.lang3.time.DateParser.getPattern.

Gets the pattern used by this parser.

private FastDateParser.Strategy

Returns:

The Strategy that will handle parsing for the field
getStrategy
(final char
A sub-sequence of the SimpleDateFormat pattern
f
,
final int
formatting width
width
,
final Calendar
The calendar to obtain the short and long values
definingCalendar
)

Gets a Strategy given a field from a SimpleDateFormat pattern

public TimeZone
getTimeZone()

Implements org.apache.commons.lang3.time.DateParser.getTimeZone.

Gets the time zone used by this parser.

public int

Returns:

a hash code compatible with equals
hashCode
()

Overrides java.lang.Object.hashCode.

Returns a hash code compatible with equals.

private void
init(final Calendar
the java.util.Calendar instance used to initialize this FastDateParser
definingCalendar
)

Initializes derived fields from defining fields.

public Date
parse(final String source)

Implements org.apache.commons.lang3.time.DateParser.parse.

Equivalent to DateFormat.parse(String).

public Date
parse(final String source, final ParsePosition pos)

Implements org.apache.commons.lang3.time.DateParser.parse.

This implementation updates the ParsePosition if the parse succeeds.

public boolean

Returns:

true, if source has been parsed (pos parsePosition is updated); otherwise false (and pos errorIndex is updated)
parse
(final String
The text to parse.
source
,
final ParsePosition
On input, the position in the source to start parsing, on output, updated position.
pos
,
final Calendar
The calendar into which to set parsed fields.
calendar
)

Implements org.apache.commons.lang3.time.DateParser.parse.

Parses a formatted date string according to the format.

public Object
parseObject(final String source)

Implements org.apache.commons.lang3.time.DateParser.parseObject.

Parses text from a string to produce a Date.

public Object
parseObject(final String source, final ParsePosition pos)

Implements org.apache.commons.lang3.time.DateParser.parseObject.

Parses a date/time string according to the given parse position.

private void
readObject(final ObjectInputStream
ObjectInputStream from which the object is being deserialized.
in
)

Creates the object after serialization.

private static StringBuilder
simpleQuote(final StringBuilder sb, final String value)

public String

Returns:

a debugging string
toString
()

Overrides java.lang.Object.toString.

Gets a string version of this formatter.

public String

Returns:

a string.
toStringAll
()

Converts all state of this instance to a String handy for debugging.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait