Package com.globalmentor.text
Class W3CDateFormat
- java.lang.Object
-
- java.text.Format
-
- java.text.DateFormat
-
- java.text.SimpleDateFormat
-
- com.globalmentor.text.W3CDateFormat
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
public class W3CDateFormat extends java.text.SimpleDateFormat
Class for formatting dates and times according to the W3C Note, "Date and Time Formats", http://www.w3.org/TR/NOTE-datetime, a profile of ISO 8601.Warning: The W3C patterns currently format the time zone according to RFC 822 (+/-HHmm) rather than as specified by W3C NOTE-datetime (+/-HH:mm). The decimal portions of seconds may also not be correct.
- Author:
- Garret Wilson
- See Also:
- Date and Time Formats, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
W3CDateFormat.Style
The specific style of W3C date format.
-
Field Summary
-
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
Constructors Constructor Description W3CDateFormat()
Default constructor specifying a fullW3CDateFormat.Style.DATE_TIME
format.W3CDateFormat(W3CDateFormat.Style style)
Constructs a W3C date and time formatter using the given style.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.lang.String
format(java.util.Date date, W3CDateFormat.Style style)
Formats a date by creating a W3C date and time formatter using the given style.java.lang.StringBuffer
format(java.util.Date date, java.lang.StringBuffer toAppendTo, java.text.FieldPosition fieldPosition)
Formats a Date into a date/time string.protected static java.lang.String
getPattern(W3CDateFormat.Style style)
Determines a pattern to use for the given style.protected W3CDateFormat.Style
getStyle()
java.util.Date
parse(java.lang.String source, java.text.ParsePosition pos)
Parse a date/time string according to the given parse position.-
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
-
-
-
-
Constructor Detail
-
W3CDateFormat
public W3CDateFormat()
Default constructor specifying a fullW3CDateFormat.Style.DATE_TIME
format.
-
W3CDateFormat
public W3CDateFormat(W3CDateFormat.Style style)
Constructs a W3C date and time formatter using the given style.- Parameters:
style
- One of the styles defined by this class
-
-
Method Detail
-
getStyle
protected W3CDateFormat.Style getStyle()
- Returns:
- The style to use for formatting.
-
getPattern
protected static java.lang.String getPattern(W3CDateFormat.Style style)
Determines a pattern to use for the given style. This pattern may be incomplete and the output may require more processing.- Parameters:
style
- One of the styles defined by this class- Returns:
- A pattern to use as the basis for formatting.
-
format
public java.lang.StringBuffer format(java.util.Date date, java.lang.StringBuffer toAppendTo, java.text.FieldPosition fieldPosition)
Formats a Date into a date/time string.- Overrides:
format
in classjava.text.SimpleDateFormat
- Parameters:
date
- a Date to be formatted into a date/time string.toAppendTo
- the string buffer for the returning date/time string.fieldPosition
- keeps track of the position of the field within the returned string. On input: an alignment field, if desired. On output: the offsets of the alignment field. For example, given a time text "1996.07.10 AD at 15:08:56 PDT", if the given fieldPosition is DateFormat.YEAR_FIELD, the begin index and end index of fieldPosition will be set to 0 and 4, respectively. Notice that if the same time field appears more than once in a pattern, the fieldPosition will be set for the first occurrence of that time field. For instance, formatting a Date to the time string "1 PM PDT (Pacific Daylight Time)" using the pattern "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD, the begin index and end index of fieldPosition will be set to 5 and 8, respectively, for the first occurrence of the timezone pattern character 'z'.- Returns:
- the formatted date/time string.
-
parse
public java.util.Date parse(java.lang.String source, java.text.ParsePosition pos)
Parse a date/time string according to the given parse position. For example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date that is equivalent to Date(837039928046).By default, parsing is lenient: If the input is not in the form used by this object's format method but can still be parsed as a date, then the parse succeeds. Clients may insist on strict adherence to the format by calling setLenient(false).
- Overrides:
parse
in classjava.text.SimpleDateFormat
- Parameters:
source
- The date/time string to be parsedpos
- On input, the position at which to start parsing; on output, the position at which parsing terminated, or the start position if the parse failed.- Returns:
- A Date, or null if the input could not be parsed
- See Also:
DateFormat.setLenient(boolean)
-
format
public static java.lang.String format(java.util.Date date, W3CDateFormat.Style style)
Formats a date by creating a W3C date and time formatter using the given style.- Parameters:
date
- The date to be formatted.style
- One of the W3C date/time styles.- Returns:
- A string representing the date after the formatting.
-
-