format method

String format(
  1. String formatString
)

Format the date in a string given a format string.

Explicit Pattern Syntax: Formats can also be specified with a pattern string. This can be used for formats that don't have a skeleton available, but these will not adapt to different locales. For example, in an explicit pattern the letters 'H' and 'h' are available for 24 hour and 12 hour time formats respectively. But there isn't a way in an explicit pattern to get the behaviour of the 'j' skeleton, which prints 24 hour or 12 hour time according to the conventions of the locale, and also includes am/pm markers where appropriate. So it is preferable to use the skeletons.

The following characters are available in explicit patterns:

Symbol   Meaning                Presentation       Example
------   -------                ------------       -------
G        era designator         (Text)             AD
y        year                   (Number)           1996
M        month in year          (Text & Number)    July & 07
L        standalone month       (Text & Number)    July & 07
d        day in month           (Number)           10
c        standalone day         (Number)           10
h        hour in am/pm (1~12)   (Number)           12
H        hour in day (0~23)     (Number)           0
m        minute in hour         (Number)           30
s        second in minute       (Number)           55
S        fractional second      (Number)           978
E        day of week            (Text)             Tuesday
D        day in year            (Number)           189
a        am/pm marker           (Text)             PM
k        hour in day (1~24)     (Number)           24
K        hour in am/pm (0~11)   (Number)           0
z        time zone              (Text)             Pacific Standard Time
Z        time zone (RFC 822)    (Number)           -0800
v        time zone (generic)    (Text)             Pacific Time
Q        quarter                (Text)             Q3
'        escape for text        (Delimiter)        'Date='
''       single quote           (Literal)          'o''clock'

The count of pattern letters determine the format.

Text:

  • 5 pattern letters--use narrow form for standalone. Otherwise not used.
  • 4 or more pattern letters--use full form,
  • 3 pattern letters--use short or abbreviated form if one exists
  • less than 3--use numeric form if one exists

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 & Number): 3 or over, use text, otherwise use number.

Any characters not in the pattern will be treated as quoted text. For instance, characters like ':', '.', ' ', '#' and '@' will appear in the resulting text even though they are not enclosed in single quotes. In our current pattern usage, not all letters have meanings. But those unused letters are strongly discouraged to be used as quoted text without quotes, because we may use other letters as pattern characters in the future.

Examples Using the US Locale:

Format Pattern                    Result
--------------                    -------
'yyyy.MM.dd G 'at' HH:mm:ss vvvv' 1996.07.10 AD at 15:08:56 Pacific Time
'EEE, MMM d, ''yy'                Wed, Jul 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

When parsing a date string using the abbreviated year pattern ('yy'), DateFormat 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 parse function is called. For example, using a pattern of 'MM/dd/yy' and a DateParse 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 will be parsed into the default century. Any other numeric string, such as a one digit string, a three or more digit string will be interpreted as its face value.

If the year pattern does not have exactly two 'y' characters, the year is interpreted literally, regardless of the number of digits. So using the pattern 'MM/dd/yyyy', '01/11/12' parses to Jan 11, 12 A.D.

Implementation

String format(String formatString) {
  return DateFormat(formatString).format(dateLocal);
}