com.redfin.sitemapgenerator
Class W3CDateFormat

java.lang.Object
  extended by java.text.Format
      extended by java.text.DateFormat
          extended by java.text.SimpleDateFormat
              extended by com.redfin.sitemapgenerator.W3CDateFormat
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class W3CDateFormat
extends java.text.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

Nested Class Summary
static class W3CDateFormat.Pattern
          The six patterns defined by W3C, plus W3CDateFormat.Pattern.AUTO configuration
 
Nested classes/interfaces inherited from class java.text.DateFormat
java.text.DateFormat.Field
 
Field Summary
static java.util.TimeZone ZULU
          The GMT ("zulu") time zone, for your convenience
 
Fields inherited from class java.text.DateFormat
AM_PM_FIELD, calendar, DATE_FIELD, DAY_OF_WEEK_FIELD, DAY_OF_WEEK_IN_MONTH_FIELD, DAY_OF_YEAR_FIELD, DEFAULT, ERA_FIELD, FULL, HOUR_OF_DAY0_FIELD, HOUR_OF_DAY1_FIELD, HOUR0_FIELD, HOUR1_FIELD, LONG, MEDIUM, MILLISECOND_FIELD, MINUTE_FIELD, MONTH_FIELD, numberFormat, SECOND_FIELD, SHORT, TIMEZONE_FIELD, WEEK_OF_MONTH_FIELD, WEEK_OF_YEAR_FIELD, YEAR_FIELD
 
Constructor Summary
W3CDateFormat()
          Build a formatter in AUTO mode
W3CDateFormat(W3CDateFormat.Pattern pattern)
          Build a formatter using the specified Pattern, or AUTO mode
 
Method Summary
 java.lang.StringBuffer format(java.util.Date date, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
          This is what you override when you extend DateFormat; use DateFormat.format(Date) instead
 java.util.Date parse(java.lang.String text, java.text.ParsePosition pos)
          This is what you override when you extend DateFormat; use DateFormat.parse(String) instead
 
Methods inherited from class java.text.SimpleDateFormat
applyLocalizedPattern, applyPattern, clone, equals, formatToCharacterIterator, get2DigitYearStart, getDateFormatSymbols, hashCode, set2DigitYearStart, setDateFormatSymbols, toLocalizedPattern, toPattern
 
Methods inherited from class java.text.DateFormat
format, format, getAvailableLocales, getCalendar, getDateInstance, getDateInstance, getDateInstance, getDateTimeInstance, getDateTimeInstance, getDateTimeInstance, getInstance, getNumberFormat, getTimeInstance, getTimeInstance, getTimeInstance, getTimeZone, isLenient, parse, parseObject, setCalendar, setLenient, setNumberFormat, setTimeZone
 
Methods inherited from class java.text.Format
format, parseObject
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ZULU

public static final java.util.TimeZone ZULU
The GMT ("zulu") time zone, for your convenience

Constructor Detail

W3CDateFormat

public W3CDateFormat()
Build a formatter in AUTO mode


W3CDateFormat

public W3CDateFormat(W3CDateFormat.Pattern pattern)
Build a formatter using the specified Pattern, or AUTO mode

Method Detail

format

public java.lang.StringBuffer format(java.util.Date date,
                                     java.lang.StringBuffer toAppendTo,
                                     java.text.FieldPosition pos)
This is what you override when you extend DateFormat; use DateFormat.format(Date) instead

Overrides:
format in class java.text.SimpleDateFormat

parse

public java.util.Date parse(java.lang.String text,
                            java.text.ParsePosition pos)
This is what you override when you extend DateFormat; use DateFormat.parse(String) instead

Overrides:
parse in class java.text.SimpleDateFormat


Copyright © 2009. All Rights Reserved.