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 (for example, +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 (for example, +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