Class FastDateParser

  • All Implemented Interfaces:
    Serializable, DateParser

    public class FastDateParser
    extends Object
    implements DateParser, 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.

    Since:
    3.2
    See Also:
    FastDatePrinter, Serialized Form
    • Method Detail

      • equals

        public boolean equals​(Object obj)
        Compares another object for equality with this object.
        Overrides:
        equals in class Object
        Parameters:
        obj - the object to compare to
        Returns:
        trueif equal to this instance
      • hashCode

        public int hashCode()
        Returns a hash code compatible with equals.
        Overrides:
        hashCode in class Object
        Returns:
        a hash code compatible with equals
      • toString

        public String toString()
        Gets a string version of this formatter.
        Overrides:
        toString in class Object
        Returns:
        a debugging string
      • toStringAll

        public String toStringAll()
        Converts all state of this instance to a String handy for debugging.
        Returns:
        a string.
        Since:
        3.12.0
      • parse

        public boolean parse​(String source,
                             ParsePosition pos,
                             Calendar calendar)
        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.
        Specified by:
        parse in interface DateParser
        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:
        IllegalArgumentException - when Calendar has been set to be not lenient, and a parsed field is out of range.