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 DateTimeFormatter class from the java.time package handles these issues. Pick one of the formatting styles shown in Tables 7.4. Then, get a formatter:
FormatStyle style = . . .; // One of FormatStyle.SHORT, FormatStyle.MEDIUM, . . .
DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(style);
DateTimeFormatter timeFormatter = DateTimeFormatter.ofLocalizedTime(style);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(style);
// or DateTimeFormatter.ofLocalizedDateTime(style1, style2)
These formatters use the current locale. To use a different locale, use the withLocale method:
DateTimeFormatter dateFormatter =
DateTimeFormatter.ofLocalizedDate(style).withLocale(locale);
Now you can format a LocalDate, LocalDateTime, LocalTime, or ZonedDateTime:
ZonedDateTime appointment = . . .;
String formatted = formatter.format(appointment);
You can use one of the static parse methods of LocalDate, LocalDateTime, LocalTime, or ZonedDateTime to parse a date or time in a string:
LocalTime time = LocalTime.parse(“9:32 AM”, formatter);
These methods are not suitable for parsing human input, at least not without preprocessing. For example, the short time formatter for the United States will parse “9:32 AM” but not “9:32AM” or “9:32 am”.
Sometimes, you need to display just the names of weekdays and months, for example, in a calendar application. Call the getDisplayName method of the DayOfWeek and Month enumerations.
for (Month m : Month.values())
System.out.println(m.getDisplayName(textStyle, locale) + ” “);
Tables 7.5 shows the text styles. The STANDALONE versions are for display outside a formatted date. For example, in Finnish, January is “tammikuuta” inside a date, but “tammikuu” standalone.
Listing 7.2 shows the DateFormat class in action. You can select a locale and see how the date and time are formatted in different places around the world.
Figure 7.2 shows the program (after Chinese fonts were installed). As you can see, it correctly displays the output.
You can also experiment with parsing. Enter a date, time, or date/time and click the Parse button.
We use a helper class EnumCombo to solve a technical problem (see Listing 7.3). We wanted to fill a combo with values such as Short, Medium, and Long and then automatically convert the user’s selection to values FormatStyle.SHORT, FormatStyle.MEDIUM, and FormatStyle.LONG. Instead of writing repetitive code, we use reflection: We convert the user’s choice to upper case, replace all spaces with underscores, and then find the value of the static field with that name. (See Volume I, Chapter 5 for more details about reflection.)
Source: Horstmann Cay S. (2019), Core Java. Volume II – Advanced Features, Pearson; 11th edition.