Java Reference
In-Depth Information
Table 13-8. ( continued )
Conversion Suffix
Description
'Y'
At least a four-digit year. It adds leading zeros if the year contains less than four digits. For
example, if the year is 789, it will output 0789; if the year is 2011, it will output 2011; if the year
is 20189, it will output 20189.
'y'
The last two digits of the year. It adds leading zero if necessary. For example, if the year is 9, it
will output 09; if the year is 123, it will output 23; if the year is 2011, it will output 11.
'j'
A three-digit day of the year. Valid values are 000 to 366.
'm'
A two-digit month. Valid values are 01 to 13. The special value of 13 is required to support
lunar calendar.
'd'
A two-digit day of the month. Valid values are 01 to 31.
'e'
Day of the month. Valid values are 1 to 31. It behaves the same as 'd' except that it does not add
a leading zero to the output.
Table 13-9. List of Suffix Characters for Date/Time Formatting
Conversion
Suffix
Description
'R'
It formats time in 24-hour clock format as "hour:minute" . Its effects is the same as using
"%tH:%tM" as a format specifier. Examples are "11:23" , "01:35" , "21:30" , etc.
'T'
It formats time in 24-hour clock format as " h our:minute:second" . Its effects is the same as using
"%tH:%tM:%tS" as a format specifier. Examples are "11:23:10" , "01:35:01" , "21:30:34" , etc.
'r'
It formats time in 12-hour clock format as "hour:minute:second morning/afternoon marker" .
Its effects is the same as using "%tI:%tM:%tS %Tp" as a format specifier. The morning/afternoon
marker may be locale-specific. "09:23:45 AM" , "09:30:00 PM" , etc. are example for US locale.
'D'
It formats the date as "%tm/%td/%ty" , such as "01/19/11" .
'F'
It formats the date as "%tY-%tm-%td" , such as " 2011-01-19" .
'c'
It formats the date and time as “%ta %tb %td %tT %tZ %tY”, such as “Wed Jan 19 11:52:06 CST 2011”.
The data/time conversion applies localization wherever it is applicable. The following snippet of code formats the
same date and time, January 25, 2014 11:48:16 AM, in US, Indian, and Thai locales. Note the use of the '<' flag in the
format specifier. It lets you use the argument that holds the date and time value in multiple format specifiers.
Locale englishUS = Locale.US;
Locale hindiIndia = new Locale ("hi", "IN");
Locale thaiThailand = new Locale ("th", "TH", "TH");
// Construct a LocalDateTime
LocalDateTime ldt = LocalDateTime.of(2014, Month.JANUARY, 25, 11, 48, 16);
System.out.printf(englishUS, "In US: %tB %<te, %<tY %<tT %<Tp%n", ldt);
System.out.printf(hindiIndia, "In India: %tB %<te, %<tY %<tT %<Tp%n", ldt);
System.out.printf(thaiThailand, "In Thailand: %tB %<te, %<tY %<tT %<Tp%n", ldt);
Search WWH ::




Custom Search