Class DateTimeFormat

public class DateTimeFormat
extends Object
Formats and parses dates and times using locale-sensitive patterns.

Patterns

SymbolMeaningPresentationExample
Gera designatorTextAD
yyearNumber1996
Mmonth in yearText or NumberJuly (or) 07
dday in monthNumber10
hhour in am/pm (1-12)Number12
Hhour in day (0-23)Number0
mminute in hourNumber30
ssecond in minuteNumber55
Sfractional secondNumber978
Eday of weekTextTuesday
aam/pm markerTextPM
khour in day (1-24)Number24
Khour in am/pm (0-11)Number0
ztime zoneTextPacific Standard Time
Ztime zone (RFC 822)Number-0800
vtime zone (generic)TextPacific Time
'escape for textDelimiter'Date='
''single quoteLiteral'o''clock'

The number of pattern letters influences the format, as follows:

Text
if 4 or more, then use the full form; if less than 4, use short or abbreviated form if it exists (e.g., "EEEE" produces "Monday", "EEE" produces "Mon")
Number
the minimum number of digits. Shorter numbers are zero-padded to this amount (e.g. if "m" produces "6", "mm" produces "06"). Year is handled specially; that is, if the count of 'y' is 2, the Year will be truncated to 2 digits. (e.g., if "yyyy" produces "1997", "yy" produces "97".) Unlike other fields, fractional seconds are padded on the right with zero.
Text or Number
3 or more, use text, otherwise use number. (e.g. "M" produces "1", "MM" produces "01", "MMM" produces "Jan", and "MMMM" produces "January".

Any characters in the pattern that are not in the ranges of ['a'..'z'] and ['A'..'Z'] will be treated as quoted text. For instance, characters like ':', '.', '' (space), '#' and '@' will appear in the resulting time text even they are not embraced within single quotes.

Parsing Dates and Times

This implementation could parse partial date/time. Current date will be used to fill in the unavailable date part. 00:00:00 will be used to fill in the time part.

As with formatting (described above), the count of pattern letters determine the parsing behavior.

Text
4 or more pattern letters--use full form, less than 4--use short or abbreviated form if one exists. In parsing, we will always try long format, then short.
Number
the minimum number of digits.
Text or Number
3 or more characters means use text, otherwise use number

Although the current pattern specification doesn't not specify behavior for all letters, it may in the future. It is strongly discouraged to used unspecified letters as literal text without being surrounded by quotes.

Examples

PatternFormatted Text
"yyyy.MM.dd G 'at' HH:mm:ss vvvv"1996.07.10 AD at 15:08:56 Pacific Time
"EEE, MMM d, ''yy"Wed, July 10, '96
"h:mm a"12:08 PM
"hh 'o''clock' a, zzzz" 12 o'clock PM, Pacific Daylight Time
"K:mm a, vvv" 0:00 PM, PT
"yyyyy.MMMMM.dd GGG hh:mm aaa"01996.July.10 AD 12:08 PM

Additional Parsing Considerations

When parsing a date string using the abbreviated year pattern ("yy"), the parser must interpret the abbreviated year relative to some century. It does this by adjusting dates to be within 80 years before and 20 years after the time the parser instance is created. For example, using a pattern of "MM/dd/yy" and a DateTimeFormat object created on Jan 1, 1997, the string "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64" would be interpreted as May 4, 1964. During parsing, only strings consisting of exactly two digits, as defined by Character.isDigit(char), will be parsed into the default century. If the year pattern does not have exactly two 'y' characters, the year is interpreted literally, regardless of the number of digits. For example, using the pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.

When numeric fields abut one another directly, with no intervening delimiter characters, they constitute a run of abutting numeric fields. Such runs are parsed specially. For example, the format "HHmmss" parses the input text "123456" to 12:34:56, parses the input text "12345" to 1:23:45, and fails to parse "1234". In other words, the leftmost field of the run is flexible, while the others keep a fixed width. If the parse fails anywhere in the run, then the leftmost field is shortened by one character, and the entire run is parsed again. This is repeated until either the parse succeeds or the leftmost field is one character in length. If the parse still fails at that point, the parse of the run fails.

In the current implementation, timezone parsing only supports GMT:hhmm, GMT:+hhmm, and GMT:-hhmm.

Constructors

DateTimeFormat(String)Constructs a format object using the specified pattern and the date time constants for the default locale.
DateTimeFormat(String, DateTimeConstants)Constructs a format object using the specified pattern and user-supplied date time constants.

Methods

format(Date)Format a date object.
getFormat(String)Returns a format object using the specified pattern and the date time constants for the default locale.
getFullDateFormat()
getFullDateTimeFormat()
getFullTimeFormat()
getLongDateFormat()
getLongDateTimeFormat()
getLongTimeFormat()
getMediumDateFormat()
getMediumDateTimeFormat()
getMediumTimeFormat()
getPattern()
getShortDateFormat()
getShortDateTimeFormat()
getShortTimeFormat()
parse(String)Parses text to produce a Date value.
parse(String, int, Date)This method parses the input string, fill its value into a Date.

Constructor Detail

DateTimeFormat

protected DateTimeFormat(String pattern)
Constructs a format object using the specified pattern and the date time constants for the default locale.

Parameters

pattern
string pattern specification

DateTimeFormat

protected DateTimeFormat(String pattern, DateTimeConstants dateTimeConstants)
Constructs a format object using the specified pattern and user-supplied date time constants.

Parameters

pattern
string pattern specification
dateTimeConstants
locale specific symbol collection

Method Detail

format

public String format(Date date)
Format a date object.

Parameters

date
the date object being formatted

Return Value

formatted date representation

getFormat

public static DateTimeFormat getFormat(String pattern)
Returns a format object using the specified pattern and the date time constants for the default locale. If you need to format or parse repeatedly using the same pattern, it is highly recommended that you cache the returned DateTimeFormat object and reuse it rather than calling this method repeatedly.

Parameters

pattern
string to specify how the date should be formatted

Return Value

a DateTimeFormat object that can be used for format or parse date/time values matching the specified pattern

getFullDateFormat

public static DateTimeFormat getFullDateFormat()

getFullDateTimeFormat

public static DateTimeFormat getFullDateTimeFormat()

getFullTimeFormat

public static DateTimeFormat getFullTimeFormat()

getLongDateFormat

public static DateTimeFormat getLongDateFormat()

getLongDateTimeFormat

public static DateTimeFormat getLongDateTimeFormat()

getLongTimeFormat

public static DateTimeFormat getLongTimeFormat()

getMediumDateFormat

public static DateTimeFormat getMediumDateFormat()

getMediumDateTimeFormat

public static DateTimeFormat getMediumDateTimeFormat()

getMediumTimeFormat

public static DateTimeFormat getMediumTimeFormat()

getPattern

public String getPattern()

getShortDateFormat

public static DateTimeFormat getShortDateFormat()

getShortDateTimeFormat

public static DateTimeFormat getShortDateTimeFormat()

getShortTimeFormat

public static DateTimeFormat getShortTimeFormat()

parse

public Date parse(String text)
Parses text to produce a Date value. An IllegalArgumentException is thrown if either the text is empty or if the parse does not consume all characters of the text.

Parameters

text
the string being parsed

Return Value

a parsed date/time value

parse

public int parse(String text, int start, Date date)
This method parses the input string, fill its value into a Date.

Parameters

text
the string that need to be parsed
start
the character position in "text" where parsing should start
date
the date object that will hold parsed value

Return Value

0 if parsing failed, otherwise the number of characters advanced