Slide navigation: Forward with space bar, → arrow key, or PgDn. Backwards with ← or PgUp.

Copyright © Cay S. Horstmann 2016
123,456.78 (United States) 123.456,78 (Switzerland) ๑๒๓,๔๕๖.๗๘ (Thailand with Thai numerals)
March 22, 1961 22. März 1961 1961年3月22日
de or zh.Latn, Cyrl, Hant.US or CH.u-nu-thai or x-java.Locale object from a locale string, from one of the predefined locales, or for the current locale of the user's computer:
Locale locale = Locale.forLanguageTag("en-US");
locale = Locale.CANADA_FRENCH;
locale = Locale.getDefault(Locale.Category.DISPLAY);
Locale en_US = Locale.forLanguageTag("en-US");
Locale fr_CA = Locale.CANADA_FRENCH;
en_US.getDisplayName()
en_US.getDisplayName(fr_CA)
java.text.NumberFormat class has factory methods for formatting numbers, percentages, and currencies:
Locale loc = Locale.GERMANY;
NumberFormat formatter = NumberFormat.getCurrencyInstance(loc);
double amt = 123456.78;
String result = formatter.format(amt);
// Yields 123.456,78€Currency class for currencies that are from a different locale:
formatter.setCurrency(Currency.getInstance("USD"));
System.out.println(formatter.format(dollars));NumberFormat.parse method:
String input = "123.456,78€"; Number parsed = formatter.parse(input); double x = parsed.doubleValue();
NumberFormatException is thrown.Scanner, you can set its locale with the useLocale method.String.format and PrintWriter.printf methods take a Locale argument.import java.text.*;
Locale de_DE = Locale.GERMANY;
NumberFormat formatter = NumberFormat.getCurrencyInstance(de_DE);
double amt = 123456.78;
formatter.format(amt)
NumberFormat.getCurrencyInstance(Locale.US).format(amt)
NumberFormat.getCurrencyInstance(Locale.forLanguageTag("th-TH-u-nu-thai")).format(amt)
java.time.DateTimeFormatter, not the legacy java.text.DateFormat class.| Style | Date | Time |
|---|---|---|
SHORT |
7/16/69 |
9:32 AM |
MEDIUM |
Jul 16, 1969 |
9:32:00 AM |
LONG |
July 16, 1969 |
9:32:00 AM EDT |
FULL |
Wednesday, July 16, 1969 |
9:32:00 AM EDT |
DateTimeFormatter formatter =
DateTimeFormatter.ofLocalizedDateTime(style).withLocale(locale);
// Or ofLocalizedDate/ofLocalizedTime
LocalDate, LocalDateTime, LocalTime, or ZonedDateTime:
ZonedDateTime appointment = ...; String formatted = formatter.format(appointment);
parse method:
LocalTime time = LocalTime.parse("9:32 AM", formatter);import java.time.*; import java.time.format.*; DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG); LocalDate now = LocalDate.now(); formatter.withLocale(Locale.US).format(now);
Repeat with Locale.FRENCH and Locale.CHINESE
| Style | Example |
|---|---|
FULL / FULL_STANDALONE |
January |
SHORT / SHORT_STANDALONE |
Jan |
NARROW / NARROW_STANDALONE |
J |
STANDALONE versions are for display outside
a formatted date.
getDisplayName methods of the DayOfWeek and Month enumerations:
for (Month m : Month.values()) System.out.println(m.getDisplayName(textStyle, locale));
DayOfWeek first = WeekFields.of(locale).getFirstDayOfWeek();
import java.time.*;
import java.time.format.*;
import java.util.stream.*;
List<String> weekdayNames(Locale locale) {
return Stream.of(DayOfWeek.values()).
map(m -> m.getDisplayName(TextStyle.FULL, locale)).
collect(Collectors.toList());
}
weekdayNames(Locale.US)
weekdayNames(Locale.CHINESE)
weekdayNames(Locale.forLanguageTag("fi"))
import java.time.temporal.*
WeekFields.of(Locale.US).getFirstDayOfWeek()
WeekFields.of(Locale.GERMAN).getFirstDayOfWeek()
String.compareTo for comparing humanly readable strings.Athens Zulu able zebra Ångström
Å comes after Z.Collator class implements the Comparator<Object> interface:
Collator coll = Collator.getInstance(locale); words.sort(coll);
setStrength to make the collator less selective.
Collator.PRIMARY: e ≠ f, e = é, e = ECollator.SECONDARY: e ≠ f, e ≠ é, e = ECollator.TERTIARY (default): e ≠ f, e ≠ é, e ≠ E java.text.Normalizer to compute the normalized form of any string:
String city = "San Jos\u0301"; String normalized = Normalizer.normalize(city, Normalizer.Form.NFC);
setDecomposition to make the collator less selective.
Collator.FULL_DECOMPOSITION: é = e´, ™ = TMCollator.NO_DECOMPOSITION (default): é = e´, ™ ≠ TMCollator.IDENTICAL: é ≠ e´, ™ ≠ TMLocale argument when calling toUpperCase/toLowerCase:
String street = "Einbahnstraße"
String uppercase = street.toUpperCase(Locale.GERMAN);
// "EINBAHNSTRASSE"
s.toLowerCase() and s.toUpperCase().toLowerCase() may be different.MessageFormat.format with placeholders:
String template = "{0} has {1} messages";
// In French, we'll use "Il y a {1} messages pour {0}"
String text = MessageFormat.format(template, "Pierre", 42);
format method with the default locale. For other locales, construct a MessageFormat object:
MessageFormat mf = new MessageFormat(template, locale);
String text = mf.format(new Object[] { "Pierre", 42 }); // No varargs ☹number and currency, integer, percent, or a number format pattern of the DecimalFormat class (such as $,##0) to the placeholder:
template="Your current total is {0,number,currency}."
// Will yield a string such as "Your current total is $1023.95."date or
time, followed by the format short, medium, long, or full, or a format pattern of the
SimpleDateFormat class (such as yyyy-MM-dd):
String message = MessageFormat("It is now {0,time,short}.", Date.from(Instant.now()));java.time objects to the legacy java.util.Date type.No files copied 1 file copied 42 files copied
lowerLimit#formatString
| symbols:
String template = "{0,choice,0#No files|1#1 file|2#{0} files} copied";0 appears twice: to make the choice, and to insert the count into the third choice.≤ instead of #, < for a “less than” bound, and -∞ for the unused first bound:
-∞<No files|0<1 file|2≤{0} files
'' to include single quotes in the template:
String template = "<a href=''{0}''>{1}</a>";
ResourceBundle..properties and contains key/value pairs:
computeButton=Rechnen cancelButton=Abbrechen defaultPaperSize=A4
bundleName_language_script_countrybundleName_de_CH before bundleName_decom.mycompany.messages into com/mycompany directory.ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale);
String computeButtonLabel = bundle.getString("computeButton");
file.preferences=Pr\u00e9f\u00e9rences