Summary

This article covers Date/Time Formats.


Table of Contents


Date and time formats are specified by date and time pattern strings, within which unquoted letters, from ‘A’ to ‘Z’ and ‘a’ to ‘z,’ are interpreted as pattern letters representing the components of a date or time string. Text can be quoted using single quotes (‘) to avoid interpretation. All other characters are not interpreted; they are simply copied into the output string during formatting, or matched against the input string during parsing. The following pattern letters are defined (all other characters, from ‘A‘ to ‘Z‘ and ‘a‘ to ‘z,’ are reserved):

References

LetterDate or Time ComponentPresentationExamples
GEraTextAD
yYearYear1996, 96
MMonth in yearMonthJuly, Jul, 07
wWeek in yearNumber27
WWeek in monthNumber2
DDay in yearNumber189
dDay in monthNumber10
FDay of week in monthNumber2
EDay in weekTextTuesday, Tue
aAM/PMTextPM
HHour in day (0-23)Number0
kHour in day (1-24)Number24
hHour in am/pm (1-12)Number12
KHour in am/pm (0-11)Number0
mMinute in hourNumber30
sSecond in minuteNumber55
SMillisecondNumber978
zTime zoneGeneral time zonePacific Standard Time, PST, GMT -08:00
ZTime zoneRFC 822 time zone-0800

Pattern letters are usually repeated, as their number determines the exact presentation:

Text

For formatting, if the number of pattern letters is four or more, the full form is used; otherwise, a short or abbreviated form is used, if available. For parsing, both forms are accepted, regardless of the number of pattern letters.

Number

For formatting, the number of pattern letters is the minimum number of digits, and shorter numbers are zero-padded to this amount. For parsing, the number of pattern letters is ignored unless it is needed to separate two adjacent fields.

Year

For formatting, if the number of pattern letters is two, the year is truncated to two digits; otherwise, it is interpreted as a NumberFormat.

For parsing, if the number of pattern letters is more than two, the year is interpreted literally, regardless of the number of digits; therefore, using the pattern “MM/dd/yyyy”, “01/11/12″ parses to Jan 11, 12 A.D. For parsing with the abbreviated year pattern (“y” or “yy”), SimpleDateFormat 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 SimpleDateFormat instance is created. For example, using a pattern of “MM/dd/yy”; and a SimpleDateFormat instance 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. Any other numeric string, such as a one-digit string, a three- or more digit string, or a two-digit string that does not consist entirely of digits (e.g., “-1″), is interpreted literally. Therefore, “01/02/3″ or “01/02/003″ are parsed using the same pattern as Jan 2, 3 AD. Likewise, “01/02/-3″; is parsed as Jan 2, 4 BC.

Month

If the number of pattern letters is three or more, the month is interpreted as text; otherwise, it is interpreted as a number.

General Time Zone

Time zones are interpreted as text when they have names. For time zones representing a GMT offset value, the following syntax is used:

GMTOffsetTimeZone: GMT Sign Hours: Minutes
Sign: one of +-
Hours: Digit Digit Digit
Minutes: Digit Digit
Digit: one of 0 1 2 3 4 5 6 7 8 9
Hours: must be between 0 and 23
Minutes: must be between 00 and 59

The format is locale independent, and digits must be taken from the Basic Latin block of the Unicode standard.

RFC 822 Time Zone

RFC 822 time zones are also acceptable for parsing. For formatting, the RFC 822 4-digit time zone format is used:

RFC822TimeZone: Sign TwoDigitHours Minutes
TwoDigitHours: Digit Digit
TwoDigitHours must be between 00 and 23

Other Time Zones

Other definitions are as those for general time zones. For parsing, general time zones are also accepted. SimpleDateFormat also supports localized date and time pattern strings. In these strings, the pattern letters described above may be replaced with other, locale dependent pattern letters. SimpleDateFormat does not deal with the localization of text, other than the pattern letters; localization of text is dependent upon the client of the class.

Examples

The following examples show how date and time patterns are interpreted in the U.S. locale. The given date and time are 2001-07-04 12:08:56 local time in the U.S. Pacific time zone.

Date and Time PatternResult
yyyy.MM.dd G HH:mm:ss2001.07.04 AD 12:08:56
EEE MMM d, yyWed Jul 4, 01
hh a, zzzz12 PM, Pacific Daylight Time
K;mm a, z0:08 PM, PDT
yyyyy.MMMMM.dd GGG hh:mm aaa02001.July.04 AD 12:08 PM
EEE, d MMM yyyy HH:mm:ss ZWed, 4 Jul 2001 12:08:56 -0700
yyMMddHHmmssZ010704120856-0700

Need more help?

Please visit the Fountayn Contact Information page.