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 full 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.
Let see an example in the code snippet below:
And here are the results of our code snippet above: