5.3. Date and Time

When you are formatting date and time, you should be concerned with four locale-dependent issues:

• The names of months and weekdays should be presented in the local language.

• There will be local preferences for the order of year, month, and day.

• The Gregorian calendar might not be the local preference for expressing dates.

• The time zone of the location must be taken into account.

The Java DateFormat class handles these issues. It is easy to use and quite similar to the NumberFormat class. First, get a locale. You can use the default locale or call the static getAvailableLocales method to obtain an array of locales that support date formatting. Then, call one of the three factory methods:

Get Core Java® Volume II—Advanced Features, Ninth Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.