Class FastDateFormat

java.lang.Object
java.text.Format
org.apache.commons.lang3.time.FastDateFormat
All Implemented Interfaces:
Serializable, Cloneable, DateParser, DatePrinter

public class FastDateFormat extends Format implements DateParser, DatePrinter
FastDateFormat is a fast and thread-safe version of SimpleDateFormat.

To obtain an instance of FastDateFormat, use one of the static factory methods: getInstance(String, TimeZone, Locale), getDateInstance(int, TimeZone, Locale), getTimeInstance(int, TimeZone, Locale), or getDateTimeInstance(int, int, TimeZone, Locale)

Since FastDateFormat is thread safe, you can use a static member instance:

private static final FastDateFormat DATE_FORMATTER = FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.SHORT);

This class can be used as a direct replacement to SimpleDateFormat in most formatting and 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 have closed the bug/RFE.

All patterns are compatible with SimpleDateFormat (except time zones and some year patterns - see below).

Since 3.2, FastDateFormat supports parsing as well as printing.

Java 1.4 introduced a new pattern letter, 'Z', to represent time zones in RFC822 format (eg. +0800 or -1100). This pattern letter can be used here (on all JDK versions).

In addition, the pattern 'ZZ' has been made to represent ISO 8601 extended format time zones (eg. +08:00 or -11:00). This introduces a minor incompatibility with Java 1.4, but at a gain of useful functionality.

Javadoc cites for the year pattern: For formatting, if the number of pattern letters is 2, the year is truncated to 2 digits; otherwise it is interpreted as a number. Starting with Java 1.7 a pattern of 'Y' or 'YYY' will be formatted as '2003', while it was '03' in former Java versions. FastDateFormat implements the behavior of Java 7.

Since:
2.0
See Also:
  • Field Details

  • Constructor Details

    • FastDateFormat

      protected FastDateFormat(String pattern, TimeZone timeZone, Locale locale)
      Constructs a new FastDateFormat.
      Parameters:
      pattern - SimpleDateFormat compatible pattern
      timeZone - non-null time zone to use
      locale - non-null locale to use
      Throws:
      NullPointerException - if pattern, timeZone, or locale is null.
    • FastDateFormat

      protected FastDateFormat(String pattern, TimeZone timeZone, Locale locale, Date centuryStart)
      Constructs a new FastDateFormat.
      Parameters:
      pattern - SimpleDateFormat compatible pattern
      timeZone - non-null time zone to use
      locale - non-null locale to use
      centuryStart - The start of the 100-year period to use as the "default century" for 2 digit year parsing. If centuryStart is null, defaults to now - 80 years
      Throws:
      NullPointerException - if pattern, timeZone, or locale is null.
  • Method Details

    • getDateInstance

      public static FastDateFormat getDateInstance(int style)
      Gets a date formatter instance using the specified style in the default time zone and locale.
      Parameters:
      style - date style: FULL, LONG, MEDIUM, or SHORT
      Returns:
      a localized standard date formatter
      Throws:
      IllegalArgumentException - if the Locale has no date pattern defined
      Since:
      2.1
    • getDateInstance

      public static FastDateFormat getDateInstance(int style, Locale locale)
      Gets a date formatter instance using the specified style and locale in the default time zone.
      Parameters:
      style - date style: FULL, LONG, MEDIUM, or SHORT
      locale - optional locale, overrides system locale
      Returns:
      a localized standard date formatter
      Throws:
      IllegalArgumentException - if the Locale has no date pattern defined
      Since:
      2.1
    • getDateInstance

      public static FastDateFormat getDateInstance(int style, TimeZone timeZone)
      Gets a date formatter instance using the specified style and time zone in the default locale.
      Parameters:
      style - date style: FULL, LONG, MEDIUM, or SHORT
      timeZone - optional time zone, overrides time zone of formatted date
      Returns:
      a localized standard date formatter
      Throws:
      IllegalArgumentException - if the Locale has no date pattern defined
      Since:
      2.1
    • getDateInstance

      public static FastDateFormat getDateInstance(int style, TimeZone timeZone, Locale locale)
      Gets a date formatter instance using the specified style, time zone and locale.
      Parameters:
      style - date style: FULL, LONG, MEDIUM, or SHORT
      timeZone - optional time zone, overrides time zone of formatted date
      locale - optional locale, overrides system locale
      Returns:
      a localized standard date formatter
      Throws:
      IllegalArgumentException - if the Locale has no date pattern defined
    • getDateTimeInstance

      public static FastDateFormat getDateTimeInstance(int dateStyle, int timeStyle)
      Gets a date/time formatter instance using the specified style in the default time zone and locale.
      Parameters:
      dateStyle - date style: FULL, LONG, MEDIUM, or SHORT
      timeStyle - time style: FULL, LONG, MEDIUM, or SHORT
      Returns:
      a localized standard date/time formatter
      Throws:
      IllegalArgumentException - if the Locale has no date/time pattern defined
      Since:
      2.1
    • getDateTimeInstance

      public static FastDateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)
      Gets a date/time formatter instance using the specified style and locale in the default time zone.
      Parameters:
      dateStyle - date style: FULL, LONG, MEDIUM, or SHORT
      timeStyle - time style: FULL, LONG, MEDIUM, or SHORT
      locale - optional locale, overrides system locale
      Returns:
      a localized standard date/time formatter
      Throws:
      IllegalArgumentException - if the Locale has no date/time pattern defined
      Since:
      2.1
    • getDateTimeInstance

      public static FastDateFormat getDateTimeInstance(int dateStyle, int timeStyle, TimeZone timeZone)
      Gets a date/time formatter instance using the specified style and time zone in the default locale.
      Parameters:
      dateStyle - date style: FULL, LONG, MEDIUM, or SHORT
      timeStyle - time style: FULL, LONG, MEDIUM, or SHORT
      timeZone - optional time zone, overrides time zone of formatted date
      Returns:
      a localized standard date/time formatter
      Throws:
      IllegalArgumentException - if the Locale has no date/time pattern defined
      Since:
      2.1
    • getDateTimeInstance

      public static FastDateFormat getDateTimeInstance(int dateStyle, int timeStyle, TimeZone timeZone, Locale locale)
      Gets a date/time formatter instance using the specified style, time zone and locale.
      Parameters:
      dateStyle - date style: FULL, LONG, MEDIUM, or SHORT
      timeStyle - time style: FULL, LONG, MEDIUM, or SHORT
      timeZone - optional time zone, overrides time zone of formatted date
      locale - optional locale, overrides system locale
      Returns:
      a localized standard date/time formatter
      Throws:
      IllegalArgumentException - if the Locale has no date/time pattern defined
    • getInstance

      public static FastDateFormat getInstance()
      Gets a formatter instance using the default pattern in the default locale.
      Returns:
      a date/time formatter
    • getInstance

      public static FastDateFormat getInstance(String pattern)
      Gets a formatter instance using the specified pattern in the default locale.
      Parameters:
      pattern - SimpleDateFormat compatible pattern
      Returns:
      a pattern based date/time formatter
      Throws:
      IllegalArgumentException - if pattern is invalid
    • getInstance

      public static FastDateFormat getInstance(String pattern, Locale locale)
      Gets a formatter instance using the specified pattern and locale.
      Parameters:
      pattern - SimpleDateFormat compatible pattern
      locale - optional locale, overrides system locale
      Returns:
      a pattern based date/time formatter
      Throws:
      IllegalArgumentException - if pattern is invalid
    • getInstance

      public static FastDateFormat getInstance(String pattern, TimeZone timeZone)
      Gets a formatter instance using the specified pattern and time zone.
      Parameters:
      pattern - SimpleDateFormat compatible pattern
      timeZone - optional time zone, overrides time zone of formatted date
      Returns:
      a pattern based date/time formatter
      Throws:
      IllegalArgumentException - if pattern is invalid
    • getInstance

      public static FastDateFormat getInstance(String pattern, TimeZone timeZone, Locale locale)
      Gets a formatter instance using the specified pattern, time zone and locale.
      Parameters:
      pattern - SimpleDateFormat compatible pattern
      timeZone - optional time zone, overrides time zone of formatted date
      locale - optional locale, overrides system locale
      Returns:
      a pattern based date/time formatter
      Throws:
      IllegalArgumentException - if pattern is invalid or null
    • getTimeInstance

      public static FastDateFormat getTimeInstance(int style)
      Gets a time formatter instance using the specified style in the default time zone and locale.
      Parameters:
      style - time style: FULL, LONG, MEDIUM, or SHORT
      Returns:
      a localized standard time formatter
      Throws:
      IllegalArgumentException - if the Locale has no time pattern defined
      Since:
      2.1
    • getTimeInstance

      public static FastDateFormat getTimeInstance(int style, Locale locale)
      Gets a time formatter instance using the specified style and locale in the default time zone.
      Parameters:
      style - time style: FULL, LONG, MEDIUM, or SHORT
      locale - optional locale, overrides system locale
      Returns:
      a localized standard time formatter
      Throws:
      IllegalArgumentException - if the Locale has no time pattern defined
      Since:
      2.1
    • getTimeInstance

      public static FastDateFormat getTimeInstance(int style, TimeZone timeZone)
      Gets a time formatter instance using the specified style and time zone in the default locale.
      Parameters:
      style - time style: FULL, LONG, MEDIUM, or SHORT
      timeZone - optional time zone, overrides time zone of formatted time
      Returns:
      a localized standard time formatter
      Throws:
      IllegalArgumentException - if the Locale has no time pattern defined
      Since:
      2.1
    • getTimeInstance

      public static FastDateFormat getTimeInstance(int style, TimeZone timeZone, Locale locale)
      Gets a time formatter instance using the specified style, time zone and locale.
      Parameters:
      style - time style: FULL, LONG, MEDIUM, or SHORT
      timeZone - optional time zone, overrides time zone of formatted time
      locale - optional locale, overrides system locale
      Returns:
      a localized standard time formatter
      Throws:
      IllegalArgumentException - if the Locale has no time pattern defined
    • applyRules

      Performs the formatting by applying the rules to the specified calendar.
      Parameters:
      calendar - the calendar to format
      buf - the buffer to format into
      Returns:
      the specified string buffer
    • equals

      public boolean equals(Object obj)
      Compares two objects for equality.
      Overrides:
      equals in class Object
      Parameters:
      obj - the object to compare to
      Returns:
      true if equal
    • format

      public String format(Calendar calendar)
      Formats a Calendar object.
      Specified by:
      format in interface DatePrinter
      Parameters:
      calendar - the calendar to format
      Returns:
      the formatted string
    • format

      public <B extends Appendable> B format(Calendar calendar, B buf)
      Formats a Calendar object into the supplied StringBuffer.
      Specified by:
      format in interface DatePrinter
      Type Parameters:
      B - the Appendable class type, usually StringBuilder or StringBuffer.
      Parameters:
      calendar - the calendar to format
      buf - the buffer to format into
      Returns:
      the specified string buffer
      Since:
      3.5
    • format

      Deprecated.
      Formats a Calendar object into the supplied StringBuffer.
      Specified by:
      format in interface DatePrinter
      Parameters:
      calendar - the calendar to format
      buf - the buffer to format into
      Returns:
      the specified string buffer
    • format

      public String format(Date date)
      Formats a Date object using a GregorianCalendar.
      Specified by:
      format in interface DatePrinter
      Parameters:
      date - the date to format
      Returns:
      the formatted string
    • format

      public <B extends Appendable> B format(Date date, B buf)
      Formats a Date object into the supplied StringBuffer using a GregorianCalendar.
      Specified by:
      format in interface DatePrinter
      Type Parameters:
      B - the Appendable class type, usually StringBuilder or StringBuffer.
      Parameters:
      date - the date to format
      buf - the buffer to format into
      Returns:
      the specified string buffer
      Since:
      3.5
    • format

      Deprecated.
      Formats a Date object into the supplied StringBuffer using a GregorianCalendar.
      Specified by:
      format in interface DatePrinter
      Parameters:
      date - the date to format
      buf - the buffer to format into
      Returns:
      the specified string buffer
    • format

      public String format(long millis)
      Formats a millisecond long value.
      Specified by:
      format in interface DatePrinter
      Parameters:
      millis - the millisecond value to format
      Returns:
      the formatted string
      Since:
      2.1
    • format

      public <B extends Appendable> B format(long millis, B buf)
      Formats a millisecond long value into the supplied StringBuffer.
      Specified by:
      format in interface DatePrinter
      Type Parameters:
      B - the Appendable class type, usually StringBuilder or StringBuffer.
      Parameters:
      millis - the millisecond value to format
      buf - the buffer to format into
      Returns:
      the specified string buffer
      Since:
      3.5
    • format

      @Deprecated public StringBuffer format(long millis, StringBuffer buf)
      Deprecated.
      Formats a millisecond long value into the supplied StringBuffer.
      Specified by:
      format in interface DatePrinter
      Parameters:
      millis - the millisecond value to format
      buf - the buffer to format into
      Returns:
      the specified string buffer
      Since:
      2.1
    • format

      public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos)
      Formats a Date, Calendar or Long (milliseconds) object. This method is an implementation of Format.format(Object, StringBuffer, FieldPosition)
      Specified by:
      format in interface DatePrinter
      Specified by:
      format in class Format
      Parameters:
      obj - the object to format
      toAppendTo - the buffer to append to
      pos - the position - ignored
      Returns:
      the buffer passed in
      See Also:
    • getLocale

      public Locale getLocale()
      Gets the locale used by this formatter.
      Specified by:
      getLocale in interface DateParser
      Specified by:
      getLocale in interface DatePrinter
      Returns:
      the locale
    • getMaxLengthEstimate

      public int getMaxLengthEstimate()
      Gets an estimate for the maximum string length that the formatter will produce.

      The actual formatted length will almost always be less than or equal to this amount.

      Returns:
      the maximum formatted length
    • getPattern

      public String getPattern()
      Gets the pattern used by this formatter.
      Specified by:
      getPattern in interface DateParser
      Specified by:
      getPattern in interface DatePrinter
      Returns:
      the pattern, SimpleDateFormat compatible
    • getTimeZone

      Gets the time zone used by this formatter.

      This zone is always used for Date formatting.

      Specified by:
      getTimeZone in interface DateParser
      Specified by:
      getTimeZone in interface DatePrinter
      Returns:
      the time zone
    • hashCode

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

      public Date parse(String source) throws ParseException
      Description copied from interface: DateParser
      Equivalent to DateFormat.parse(String). See DateFormat.parse(String) for more information.
      Specified by:
      parse in interface DateParser
      Parameters:
      source - A String whose beginning should be parsed.
      Returns:
      A Date parsed from the string
      Throws:
      ParseException - if the beginning of the specified string cannot be parsed.
    • parse

      public Date parse(String source, ParsePosition pos)
      Description copied from interface: DateParser
      Equivalent to DateFormat.parse(String, ParsePosition). See DateFormat.parse(String, ParsePosition) for more information.
      Specified by:
      parse in interface DateParser
      Parameters:
      source - A String, part of which should be parsed.
      pos - A ParsePosition object with index and error index information as described above.
      Returns:
      A Date parsed from the string. In case of error, returns null.
    • parse

      public boolean parse(String source, ParsePosition pos, Calendar calendar)
      Description copied from interface: DateParser
      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)
    • parseObject

      public Object parseObject(String source, ParsePosition pos)
      Description copied from interface: DateParser
      Parses a date/time string according to the given parse position.
      Specified by:
      parseObject in interface DateParser
      Specified by:
      parseObject in class Format
      Parameters:
      source - A String whose beginning should be parsed.
      pos - the parse position
      Returns:
      a Date object
      See Also:
    • toString

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