Class W3CDateFormat

  • All Implemented Interfaces:
    Serializable, Cloneable

    public class W3CDateFormat
    extends SimpleDateFormat

    Formats and parses dates in the six defined W3C date time formats. These formats are described in "Date and Time Formats", http://www.w3.org/TR/NOTE-datetime.

    The formats are:

    1. YEAR: YYYY (eg 1997)
    2. MONTH: YYYY-MM (eg 1997-07)
    3. DAY: YYYY-MM-DD (eg 1997-07-16)
    4. MINUTE: YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
    5. SECOND: YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
    6. MILLISECOND: YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)

    Note that W3C timezone designators (TZD) are either the letter "Z" (for GMT) or a pattern like "+00:30" or "-08:00". This is unlike RFC 822 timezones generated by SimpleDateFormat, which omit the ":" like this: "+0030" or "-0800".

    This class allows you to either specify which format pattern to use, or (by default) to automatically guess which pattern to use (AUTO mode). When parsing in AUTO mode, we'll try parsing using each pattern until we find one that works. When formatting in AUTO mode, we'll use this algorithm:

    1. If the date has fractional milliseconds (e.g. 2009-06-06T19:49:04.45Z) we'll use the MILLISECOND pattern
    2. Otherwise, if the date has non-zero seconds (e.g. 2009-06-06T19:49:04Z) we'll use the SECOND pattern
    3. Otherwise, if the date is not at exactly midnight (e.g. 2009-06-06T19:49Z) we'll use the MINUTE pattern
    4. Otherwise, we'll use the DAY pattern. If you want to format using the MONTH or YEAR pattern, you must declare it explicitly.
    Finally note that, like all classes that inherit from DateFormat, this class is not thread-safe. Also note that you can explicitly specify the timezone to use for formatting using the DateFormat.setTimeZone(TimeZone) method.
    Author:
    Dan Fabulich
    See Also:
    Date and Time Formats, Serialized Form