Provides classes and interfaces for handling text, dates, numbers, currency, messages and collation in a manner independent of natural languages. This means your main application or applet can be written to be language-independent, and it can rely upon separate, dynamically-linked localized resources. This allows the flexibility of adding localizations for new localizations at any time.

These classes are capable of formatting dates, numbers, and messages, parsing; searching and sorting strings; and iterating over characters, words, sentences, and line breaks. This package contains three main groups of classes and interfaces:

Formatting and Parsing

Formatting is the conversion of number and date objects to strings. Parsing is the reverse operation of converting strings into number and date objects.

Dates and numbers are represented internally in a locale-independent way. For example, dates are kept as milliseconds since January 1, 1970, 00:00:00 UCT. When these objects are printed or displayed, they must be converted to localized strings. The locale-specific parts of a date string, such as the time zone string, are separately imported from a locale-specific resource bundle.

The format() method converts the Date object from -604656780000 milliseconds to the form "Tuesday, November 3, 1997 9:47am CST" for the U. S. English locale. The figure shows how the format() method of subclasses of Format enable instances of Number, Date, String, and other objects to be formatted to locale-specific strings.

Conversely, the parseObject() method (and parse() method in subclasses) perform the reverse operation of parsing localized strings and converting them to Number, Date, and String objects. The figure shows how the parse() method is complementary to format(). Any String formatted by format() is guaranteed to be parseable by parseObject().

Java provides six subclasses of Format for formatting dates, numbers, and messages: DateFormat, SimpleDateFormat, NumberFormat, DecimalFormat, ChoiceFormat, and MessageFormat.

String Collation

The term "collate" means to determine the proper sort sequence for two or more strings. It can also tell if two strings match, for searching. The Collator class and its subclass RuleBasedCollator perform locale-sensitive string comparison. You use these classes to build searching and alphabetical sorting routines for natural language text. They can distinguish characters based on the base character, accent marks, and uppercase/lowercase properties.

Collator is an abstract base class. Subclasses implement specific collation strategies. One subclass, RuleBasedCollator, is currently provided and is applicable to a wide set of languages. Other subclasses may be created to handle more specialized needs. CollationElementIterator provides an iterator for stepping through each character of a locale-specific string according to the rules of a specific Collator object. CollationKey enables fast sorting of strings by representing a string as a sort key under the rules of a specific Collator object.

Word Breaks

While the Java types char and Character represent Unicode characters, sometimes Unicode characters combine to form a more complex character that has its own semantic value, a user character. The BreakIterator class makes it possible to iterate over these user characters. A break iterator can find the location of character, word, or sentence boundaries or potential line-break boundaries. This makes it possible for a program to properly select characters for text operations such as highlighting a character, cutting a word, moving to the next sentence, or word-wrapping at a line ending. These operations are performed in a locale-sensitive manner, meaning that they honor the boundaries of text for a particular locale.

Locale-Sensitive Classes

Many, but not all, of the classes in java.text are locale-sensitive, meaning you have to create a different instance for each locale. The following table lists which are locale-sensitive and which are not.

Locale-Sensitive Classes
Locale-Independent Classes

Package Specification


Related Documentation

The following classes are used by the java.text classes. For overviews, tutorials, examples, guides, and tool documentation, please see:
@since JDK1.1