Java Reference
In-Depth Information
Using User-Defined Patterns
One of the most commonly used methods in the DateTimeFormatter class is the ofPattern() method, which returns
a DateTimeFormatter object with the specified format pattern and locale.
static DateTimeFormatter ofPattern(String pattern)
static DateTimeFormatter ofPattern(String pattern, Locale locale)
The following snippet obtains two formatters to format a date in “Month day, Year” format. The first formatter
formats the datetime in the default locale and the second one in the German locale.
// Get a formatter for the default locale
DateTimeFormatter fmt1 = DateTimeFormatter.ofPattern("MMMM dd, yyyy");
// Get a formatter for the German locale
DateTimeFormatter fmt2 = DateTimeFormatter.ofPattern("MMMM dd, yyyy", Locale.GERMAN);
Sometimes you have a DateTimeFormatter object for a pattern and a locale. You want to use the same pattern
to format a datetime in another locale. The DateTimeFormatter class has a withLocale() method that returns a
DateTimeFormatter object for the specified locale that uses the same pattern. In the above snippet of code, you could
have replaced the second statement with the following one:
// Get a formatter for the German locale using the same pattern as fmt1
DateTimeFormatter fmt2 = fmt1.withLocale(Locale.GERMAN);
Use the getLocale() method of the DateTimeFormatter class to know the locale that it will use to format
datetimes.
Tip
Datetime formatting is performed based on a pattern. A formatting pattern is a sequence of characters that
have special meanings. For example, MMMM in a pattern uses the fully spelled name of a month, such as January,
February, etc.; MMM uses the short form of a month name, such as Jan, Feb, etc.; MM uses two digits month number,
such as 01, 02, etc.; M uses one or two digits month number, such as 1, 2, 10, 11, etc.
In a format pattern, some characters have special meanings and some are used literally. Characters with special
meanings will be interpreted by the formatter and they will be replaced with datetime components. A formatter
outputs the literal characters as they appear in the pattern. All letters 'A' to 'Z' and 'a' to 'z' are reserved as pattern
letters, although not all are used. If you want to include a literal string in a pattern, you need to enclose it in single
quotes. To output a single quote, you need to use two consecutive single quotes.
A datetime formatter outputs any non-letter characters, other than [, ] and a single quote, directly. However, it is
recommended that you enclose them in single quotes. Suppose you have a local date of May 29, 2012. Both patterns of
“1997 MMMM dd, yyyy” and “'1997' MMMM dd, yyyy” will output 1997 May 29, 2012; however, the latter, which uses
single quotes around the literal 1997, is recommended.
Table 12-6 lists the symbols used in patterns and their meanings. All examples in the table use
“2012-07-29T07:30:12.789-05:00[America/Chicago]” as the input datetime.
 
 
Search WWH ::




Custom Search