Java Reference
In-Depth Information
Table 10-2. Formatting codes for dates and times
Format code Meaning
Y
Year (at least four digits)
m
Month as 2-digit (leading zeros) number
B
Locale-specific month name (b for abbreviated)
d
Day of month (2 digits, leading zeros)
e
Day of month (1 or 2 digits)
A
Locale-specific day of week (a for abbreviated)
H or I
Hour in 24-hour (H) or 12-hour (I) format (2 digits, leading zeros)
M
Minute (2 digits)
S
Second (2 digits)
P/p
Locale-specific AM or PM in uppercase (P) or lowercase (p)
R or T
24-hour time combination: %tH:%tM ® or %tH:%tM:%tS (T)
D
Date formatted as “%tm/%td/%ty”
In my opinion, embedding these directly in applications that you distribute or make available
as web applications is often a bad idea, because any direct use of them assumes that you
know the correct order to print these fields in all locales around the world. Trust me, you
don't. Instead of these, I recommend the use of DateTimeFormatter , which I show you how
to use in Formatting Dates and Times , or a MessageFormat (see Formatting Messages with
MessageFormat ) to control the order of arguments. I also urge you to read all of Chapter 15
to learn about internationalization. However, for “quick and dirty” work, as well as for writ-
ing log or data files that must be in a given format because some other program reads them,
these are hard to beat.
Some date examples are shown in Example 10-3 .
Example 10-3. src/main/java/io/FormatterDates.java
Search WWH ::




Custom Search