public final class ICUHumanize extends Object
Facility for adding a "human touch" to data. It is thread-safe and supports per-thread internationalization. Additionally provides a concise facade for access to the International Components for Unicode (ICU) Java APIs.
| Modifier and Type | Method and Description |
|---|---|
static String |
compactDecimal(Number value)
Same as
compactDecimal but
defaults to SHORT compact style. |
static String |
compactDecimal(Number value,
com.ibm.icu.text.CompactDecimalFormat.CompactStyle style)
Produces abbreviated numbers.
|
static String |
compactDecimal(Number value,
com.ibm.icu.text.CompactDecimalFormat.CompactStyle style,
Locale locale)
Same as
compactDecimal for
the specified locale. |
static String |
compactDecimal(Number value,
Locale locale)
Same as
compactDecimal for the specified
locale. |
static com.ibm.icu.text.DateFormat |
dateFormatInstance(String pattern)
Returns an ICU based DateFormat instance for the current thread.
|
static com.ibm.icu.text.DateFormat |
dateFormatInstance(String pattern,
Locale locale)
Same as
dateFormatInstance for the
specified locale. |
static com.ibm.icu.text.DecimalFormat |
decimalFormatInstance(String pattern)
Returns an ICU based DecimalFormat instance for the current thread.
|
static com.ibm.icu.text.DecimalFormat |
decimalFormatInstance(String pattern,
Locale locale)
Same as
decimalFormatInstance for
the specified locale. |
static String |
duration(Number value)
Formats a number of seconds as hours, minutes and seconds.
|
static String |
format(String pattern,
Object... args)
Gets the ICU based DecimalFormat instance for the current thread with the
given pattern and uses it to format the given arguments.
|
static String |
formatCurrency(Number value)
Smartly formats the given number as a monetary amount.
|
static String |
formatCurrency(Number value,
Locale locale)
Same as
formatCurrency for the specified
locale. |
static String |
formatDate(Date value)
Same as
formatDate with SHORT style. |
static String |
formatDate(Date value,
Locale locale)
Same as
formatDate for the specified locale. |
static String |
formatDate(Date value,
String pattern)
Formats a date according to the given pattern.
|
static String |
formatDate(Date value,
String pattern,
Locale locale)
Same as
formatDate for the specified
locale. |
static String |
formatDate(int style,
Date value)
Formats the given date with the specified style.
|
static String |
formatDate(int style,
Date value,
Locale locale)
Same as
formatDate for the specified
locale. |
static String |
formatDateTime(Date value)
Formats the given date/time with SHORT style.
|
static String |
formatDateTime(Date value,
Locale locale)
Same as
formatDateTime for the specified
locale. |
static String |
formatDateTime(int dateStyle,
int timeStyle,
Date value)
Formats the given date/time with the specified styles.
|
static String |
formatDateTime(int dateStyle,
int timeStyle,
Date value,
Locale locale)
Same as
formatDateTime for the
specified locale. |
static String |
formatDecimal(Number value)
Formats the given number to the standard decimal format for the default
locale.
|
static String |
formatDecimal(Number value,
Locale locale)
Same as
formatDecimal for the specified
locale. |
static String |
formatPercent(Number value)
Formats the given ratio as a percentage.
|
static String |
formatPercent(Number value,
Locale locale)
Same as
formatPercent for the specified
locale. |
static String |
formatPluralCurrency(Number value)
Formats a monetary amount with currency plural names, for example,
"US dollar" or "US dollars" for America.
|
static String |
formatPluralCurrency(Number value,
Locale locale)
Same as
formatPluralCurrency for
the specified locale. |
static MessageFormat |
messageFormatInstance(String pattern)
Returns an ICU based MessageFormat instance for the current thread.
|
static MessageFormat |
messageFormatInstance(String pattern,
Locale locale)
Same as
messageFormatInstance for
the specified locale. |
static String |
naturalDay(Date value)
Same as
naturalDay with DateFormat.SHORT
style. |
static String |
naturalDay(Date value,
Locale locale)
Same as
naturalDay for the specified locale. |
static String |
naturalDay(int style,
Date value)
For dates that are the current day or within one day, return "today",
"tomorrow" or "yesterday", as appropriate.
|
static String |
naturalTime(Date duration)
Same as
naturalTime with current date as
reference. |
static String |
naturalTime(Date reference,
Date duration)
Computes both past and future relative dates.
|
static String |
naturalTime(Date reference,
Date duration,
Locale locale)
Same as
naturalTime for the specified
locale. |
static String |
naturalTime(Date duration,
Locale locale)
Same as
naturalTime for the specified locale. |
static String |
ordinalize(Number value)
Converts a number to its ordinal as a string.
|
static String |
ordinalize(Number value,
Locale locale)
Same as
ordinalize for the specified locale. |
static Number |
parseNumber(String text)
Converts the given text to number.
|
static Number |
parseNumber(String text,
Locale locale)
Same as
parseNumber for the specified
locale. |
static String |
pluralize(Locale locale,
String pattern,
Number value,
Object... args)
Same as
pluralize(String, Number, Object...) for the target
locale. |
static String |
pluralize(String pattern,
Number value,
Object... args)
Helper method to use ICU message patterns with Language Plural Rules
logic.
|
static String |
replaceSupplementary(String value)
Replaces characters outside the Basic Multilingual Plane with their name.
|
static String |
smartDateFormat(Date value,
String skeleton)
Guesses the best locale-dependent pattern to format the date/time fields
that the skeleton specifies.
|
static String |
smartDateFormat(Date value,
String skeleton,
Locale locale)
Same as
smartDateFormat for the specified
locale. |
static String |
spellNumber(Number value)
Converts the given number to words.
|
static String |
spellNumber(Number value,
Locale locale)
Same as
spellNumber for the specified
locale. |
static String |
transliterate(String text)
Converts the characters of the given text to latin.
|
static String |
transliterate(String text,
String id)
Converts the characters of the given text to the specified script.
|
public static String compactDecimal(Number value)
Same as compactDecimal but
defaults to SHORT compact style.
value - The number to be abbreviatedpublic static String compactDecimal(Number value, com.ibm.icu.text.CompactDecimalFormat.CompactStyle style)
value - The number to be abbreviatedstyle - The compaction stylepublic static String compactDecimal(Number value, com.ibm.icu.text.CompactDecimalFormat.CompactStyle style, Locale locale)
Same as compactDecimal for
the specified locale.
value - The number to be abbreviatedstyle - The compaction stylelocale - The localepublic static String compactDecimal(Number value, Locale locale)
Same as compactDecimal for the specified
locale.
value - The number to be abbreviatedlocale - The localepublic static com.ibm.icu.text.DateFormat dateFormatInstance(String pattern)
Returns an ICU based DateFormat instance for the current thread.
Date/Time format syntax:
The date/time format is specified by means of a string time pattern. In this pattern, all ASCII letters are reserved as pattern letters, which are defined as the following:
Symbol Meaning Presentation Example ------ ------- ------------ ------- G era designator (Text) AD y year (Number) 1996 Y year (week of year) (Number) 1997 u extended year (Number) 4601 U cyclic year name (Text,NumFallback) ren-chen (29) Q Quarter (Text & Number) Q2 & 02 M month in year (Text & Number) July & 07 d day in month (Number) 10 h hour in am/pm (1~12) (Number) 12 H hour in day (0~23) (Number) 0 m minute in hour (Number) 30 s second in minute (Number) 55 S fractional second (Number) 978 E day of week (Text) Tuesday e day of week (local 1~7) (Text & Number) Tues & 2 D day in year (Number) 189 F day of week in month (Number) 2 (2nd Wed in July) w week in year (Number) 27 W week in month (Number) 2 a am/pm marker (Text) PM k hour in day (1~24) (Number) 24 K hour in am/pm (0~11) (Number) 0 z time zone (Text) PST zzzz time zone (Text) Pacific Standard Time Z time zone (RFC 822) (Number) -0800 ZZZZ time zone (RFC 822) (Text & Number) GMT-08:00 ZZZZZ time zone (ISO 8601) (Text & Number) -08:00 & Z v time zone (generic) (Text) PT vvvv time zone (generic) (Text) Pacific Time V time zone (abreviation) (Text) PST VVVV time zone (location) (Text) United States Time (Los Angeles) g Julian day (Number) 2451334 A milliseconds in day (Number) 69540000 q stand alone quarter (Text & Number) Q2 & 02 L stand alone month (Text & Number) July & 07 c stand alone day of week (Text & Number) Tuesday & 2 ' escape for text (Delimiter) 'Date=' '' single quote (Literal) 'o''clock'
The count of pattern letters determine the format.
(Text): 4 or more, use full form, <4, use short or abbreviated form if it exists. (e.g., "EEEE" produces "Monday", "EEE" produces "Mon")
(Number): the minimum number of digits. Shorter numbers are zero-padded to this amount (e.g. if "m" produces "6", "mm" produces "06"). Year is handled specially; that is, if the count of 'y' is 2, the Year will be truncated to 2 digits. (e.g., if "yyyy" produces "1997", "yy" produces "97".) Unlike other fields, fractional seconds are padded on the right with zero.
(Text & Number): 3 or over, use text, otherwise use number. (e.g., "M" produces "1", "MM" produces "01", "MMM" produces "Jan", and "MMMM" produces "January".)
(Text,NumFallback): Behaves like Text if there is supporting data, like Number otherwise.
Any characters in the pattern that are not in the ranges of ['a'..'z'] and ['A'..'Z'] will be treated as quoted text. For instance, characters like ':', '.', ' ', '#' and '@' will appear in the resulting time text even they are not embraced within single quotes.
A pattern containing any invalid pattern letter will result in a failing UErrorCode result during formatting or parsing.
Examples using the US locale:
Format Pattern Result
-------------- -------
"yyyy.MM.dd G 'at' HH:mm:ss vvvv" ->> 1996.07.10 AD at 15:08:56 Pacific Time
"EEE, MMM d, ''yy" ->> Wed, July 10, '96
"h:mm a" ->> 12:08 PM
"hh 'o''clock' a, zzzz" ->> 12 o'clock PM, Pacific Daylight Time
"K:mm a, vvv" ->> 0:00 PM, PT
"yyyyy.MMMMM.dd GGG hh:mm aaa" ->> 1996.July.10 AD 12:08 PM
pattern - Format pattern that follows the conventions of
DateFormatpublic static com.ibm.icu.text.DateFormat dateFormatInstance(String pattern, Locale locale)
Same as dateFormatInstance for the
specified locale.
pattern - Format pattern that follows the conventions of
DateFormatlocale - Target localepublic static com.ibm.icu.text.DecimalFormat decimalFormatInstance(String pattern)
Returns an ICU based DecimalFormat instance for the current thread. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, or Indic digits. It also supports different flavors of numbers, including integers ("123"), fixed-point numbers ("123.4"), scientific notation ("1.23E4"), percentages ("12%"), and currency amounts ("$123.00", "USD123.00", "123.00 US dollars"). All of these flavors can be easily localized.
A DecimalFormat consists of a pattern and a set of
symbols. The pattern may be set directly using #applyPattern ,
or indirectly using other API methods which manipulate aspects of the
pattern, such as the minimum number of integer digits. The symbols are
stored in a DecimalFormatSymbols object. When using the NumberFormat
factory methods, the pattern and symbols are read from ICU's locale data.
Many characters in a pattern are taken literally; they are matched during parsing and output unchanged during formatting. Special characters, on the other hand, stand for other characters, strings, or classes of characters. For example, the '#' character is replaced by a localized digit. Often the replacement character is the same as the pattern character; in the U.S. locale, the ',' grouping character is replaced by ','. However, the replacement is still happening, and if the symbols are modified, the grouping character changes. Some special characters affect the behavior of the formatter by their presence; for example, if the percent character is seen, then the value is multiplied by 100 before being displayed.
To insert a special character in a pattern as a literal, that is, without any special meaning, the character must be quoted. There are some exceptions to this which are noted below.
The characters listed here are used in non-localized patterns. Localized patterns use the corresponding characters taken from this formatter's DecimalFormatSymbols object instead, and these characters lose their special status. Two exceptions are the currency sign and quote, which are not localized.
| Symbol | Location | Localized? | Meaning |
|---|---|---|---|
0
| Number | Yes | Digit |
1-9
| Number | Yes | '1' through '9' indicate rounding. |
@
| Number | No | Significant digit |
#
| Number | Yes | Digit, zero shows as absent |
.
| Number | Yes | Decimal separator or monetary decimal separator |
-
| Number | Yes | Minus sign |
,
| Number | Yes | Grouping separator |
E
| Number | Yes | Separates mantissa and exponent in scientific notation. Need not be quoted in prefix or suffix. |
+
| Exponent | Yes | Prefix positive exponents with localized plus sign. Need not be quoted in prefix or suffix. |
;
| Subpattern boundary | Yes | Separates positive and negative subpatterns |
%
| Prefix or suffix | Yes | Multiply by 100 and show as percentage |
\u2030
| Prefix or suffix | Yes | Multiply by 1000 and show as per mille |
¤ (\u00A4)
| Prefix or suffix | No | Currency sign, replaced by currency symbol. If doubled, replaced by international currency symbol. If tripled, replaced by currency plural names, for example, "US dollar" or "US dollars" for America. If present in a pattern, the monetary decimal separator is used instead of the decimal separator. |
'
| Prefix or suffix | No | Used to quote special characters in a prefix or suffix, for example,
"'#'#" formats 123 to "#123". To create a
single quote itself, use two in a row: "# o''clock".
|
*
| Prefix or suffix boundary | Yes | Pad escape, precedes pad character |
A DecimalFormat pattern contains a postive and negative
subpattern, for example, "#,##0.00;(#,##0.00)". Each subpattern has a
prefix, a numeric part, and a suffix. If there is no explicit negative
subpattern, the negative subpattern is the localized minus sign prefixed
to the positive subpattern. That is, "0.00" alone is equivalent to
"0.00;-0.00". If there is an explicit negative subpattern, it serves only
to specify the negative prefix and suffix; the number of digits, minimal
digits, and other characteristics are ignored in the negative subpattern.
That means that "#,##0.0#;(#)" has precisely the same result as
"#,##0.0#;(#,##0.0#)".
The prefixes, suffixes, and various symbols used for infinity, digits, thousands separators, decimal separators, etc. may be set to arbitrary values, and they will appear properly during formatting. However, care must be taken that the symbols and strings do not conflict, or parsing will be unreliable. For example, either the positive and negative prefixes or the suffixes must be distinct for #parse to be able to distinguish positive from negative values. Another example is that the decimal separator and thousands separator should be distinct characters, or parsing will be impossible.
The grouping separator is a character that separates clusters of integer digits to make large numbers more legible. It commonly used for thousands, but in some locales it separates ten-thousands. The grouping size is the number of digits between the grouping separators, such as 3 for "100,000,000" or 4 for "1 0000 0000". There are actually two different grouping sizes: One used for the least significant integer digits, the primary grouping size, and one used for all others, the secondary grouping size. In most locales these are the same, but sometimes they are different. For example, if the primary grouping interval is 3, and the secondary is 2, then this corresponds to the pattern "#,##,##0", and the number 123456789 is formatted as "12,34,56,789". If a pattern contains multiple grouping separators, the interval between the last one and the end of the integer defines the primary grouping size, and the interval between the last two defines the secondary grouping size. All others are ignored, so "#,##,###,####" == "###,###,####" == "##,#,###,####".
Illegal patterns, such as "#.#.#" or "#.###,###", will cause
DecimalFormat to throw an IllegalArgumentException with a
message that describes the problem.
pattern := subpattern (';' subpattern)?
subpattern := prefix? number exponent? suffix?
number := (integer ('.' fraction)?) | sigDigits
prefix := '\u0000'..'\uFFFD' - specialCharacters
suffix := '\u0000'..'\uFFFD' - specialCharacters
integer := '#'* '0'* '0'
fraction := '0'* '#'*
sigDigits := '#'* '@' '@'* '#'*
exponent := 'E' '+'? '0'* '0'
padSpec := '*' padChar
padChar := '\u0000'..'\uFFFD' - quote
Notation:
X* 0 or more instances of X
X? 0 or 1 instances of X
X|Y either X or Y
C..D any character from C up to D, inclusive
S-T characters in S, except those in T
The first subpattern is for positive numbers. The second (optional)
subpattern is for negative numbers.
Not indicated in the BNF syntax above:
padSpec may appear before the prefix,
after the prefix, before the suffix, after the suffix, or not at all.
pattern - Format pattern that follows the conventions of
DecimalFormatpublic static com.ibm.icu.text.DecimalFormat decimalFormatInstance(String pattern, Locale locale)
Same as decimalFormatInstance for
the specified locale.
pattern - Format pattern that follows the conventions of
DecimalFormatlocale - Target localepublic static String duration(Number value)
Formats a number of seconds as hours, minutes and seconds.
value - Number of secondspublic static String format(String pattern, Object... args)
Gets the ICU based DecimalFormat instance for the current thread with the given pattern and uses it to format the given arguments.
pattern - Format pattern that follows the conventions of
MessageFormatargs - Argumentspublic static String formatCurrency(Number value)
Smartly formats the given number as a monetary amount.
For en_GB:
| Input | Output |
|---|---|
| 34 | "£34" |
| 1000 | "£1,000" |
| 12.5 | "£12.50" |
value - Number to be formattedpublic static String formatCurrency(Number value, Locale locale)
Same as formatCurrency for the specified
locale.
value - Number to be formattedlocale - Target localepublic static String formatDate(Date value)
Same as formatDate with SHORT style.
value - Date to be formattedpublic static String formatDate(Date value, Locale locale)
Same as formatDate for the specified locale.
value - Date to be formattedlocale - Target localepublic static String formatDate(Date value, String pattern)
Formats a date according to the given pattern.
value - Date to be formattedpattern - The pattern. See dateFormatInstance(String)public static String formatDate(Date value, String pattern, Locale locale)
Same as formatDate for the specified
locale.
value - Date to be formattedpattern - The pattern. See dateFormatInstance(String)locale - Target localepublic static String formatDate(int style, Date value)
Formats the given date with the specified style.
style - DateFormat stylevalue - Date to be formattedpublic static String formatDate(int style, Date value, Locale locale)
Same as formatDate for the specified
locale.
style - DateFormat stylevalue - Date to be formattedlocale - Target localepublic static String formatDateTime(Date value)
Formats the given date/time with SHORT style.
value - Date to be formattedpublic static String formatDateTime(Date value, Locale locale)
Same as formatDateTime for the specified
locale.
value - Date to be formattedlocale - Target localepublic static String formatDateTime(int dateStyle, int timeStyle, Date value)
Formats the given date/time with the specified styles.
dateStyle - Date styletimeStyle - Time stylevalue - Date to be formattedpublic static String formatDateTime(int dateStyle, int timeStyle, Date value, Locale locale)
Same as formatDateTime for the
specified locale.
dateStyle - Date styletimeStyle - Time stylevalue - Date to be formattedlocale - Target localepublic static String formatDecimal(Number value)
Formats the given number to the standard decimal format for the default locale.
value - Number to be formattedpublic static String formatDecimal(Number value, Locale locale)
Same as formatDecimal for the specified
locale.
value - Number to be formattedlocale - Target localepublic static String formatPercent(Number value)
Formats the given ratio as a percentage.
| Input | Output |
|---|---|
| 0.5 | "50%" |
| 1 | "100%" |
| 0.564 | "56%" |
value - Ratio to be convertedpublic static String formatPercent(Number value, Locale locale)
Same as formatPercent for the specified
locale.
value - Ratio to be convertedlocale - Target localepublic static String formatPluralCurrency(Number value)
Formats a monetary amount with currency plural names, for example, "US dollar" or "US dollars" for America.
value - Number to be formattedpublic static String formatPluralCurrency(Number value, Locale locale)
Same as formatPluralCurrency for
the specified locale.
value - Number to be formattedlocale - Target localepublic static MessageFormat messageFormatInstance(String pattern)
Returns an ICU based MessageFormat instance for the current thread. This formatter supports a rich pattern model. For plural rules see CLDR Language Plural Rules
MessageFormat uses patterns of the following form:
MessageFormatPattern:
String
MessageFormatPattern FormatElement String
FormatElement:
{ ArgumentIndexOrName }
{ ArgumentIndexOrName , FormatType }
{ ArgumentIndexOrName , FormatType , FormatStyle }
ArgumentIndexOrName: one of
['0'-'9']+
[:ID_START:][:ID_CONTINUE:]*
FormatType: one of
number date time choice
FormatStyle:
short
medium
long
full
integer
currency
percent
SubformatPattern
String:
StringPartopt
String StringPart
StringPart:
''
' QuotedString '
UnquotedString
SubformatPattern:
SubformatPatternPartopt
SubformatPattern SubformatPatternPart
SubFormatPatternPart:
' QuotedPattern '
UnquotedPattern
Within a String, "''" represents a single quote. A
QuotedString can contain arbitrary characters except single
quotes; the surrounding single quotes are removed. An
UnquotedString can contain arbitrary characters except single
quotes and left curly brackets. Thus, a string that should result in the
formatted message "'{0}'" can be written as "'''{'0}''" or
"'''{0}'''".
Within a SubformatPattern, different rules apply. A
QuotedPattern can contain arbitrary characters except single
quotes; but the surrounding single quotes are not
removed, so they may be interpreted by the subformat. For example,
"{1,number,$'#',##}" will produce a number format with the
pound-sign quoted, with a result such as: "$#31,45". An
UnquotedPattern can contain arbitrary characters except single
quotes, but curly braces within it must be balanced. For example,
"ab {0} de" and "ab '}' de" are valid subformat
patterns, but "ab {0'}' de" and "ab } de" are
not.
The ArgumentIndex value is a non-negative integer written using
the digits '0' through '9', and represents an index into the
arguments array passed to the format methods or
the result array returned by the parse methods.
The FormatType and FormatStyle values are used to create a
Format instance for the format element. The following table
shows how the values map to Format instances. Combinations not shown in
the table are illegal. A SubformatPattern must be a valid pattern
string for the Format subclass used.
| Format Type | Format Style | Subformat Created | |||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| (none) | null
| ||||||||||||||||||||||||||||
number
| (none) | NumberFormat.getInstance(getLocale())
| |||||||||||||||||||||||||||
integer
| NumberFormat.getIntegerInstance(getLocale())
| ||||||||||||||||||||||||||||
currency
| NumberFormat.getCurrencyInstance(getLocale())
| ||||||||||||||||||||||||||||
percent
| NumberFormat.getPercentInstance(getLocale())
| ||||||||||||||||||||||||||||
| SubformatPattern |
new DecimalFormat(subformatPattern, new DecimalFormatSymbols(getLocale()))
| ||||||||||||||||||||||||||||
date
| (none) |
DateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())
| |||||||||||||||||||||||||||
short
|
DateFormat.getDateInstance(DateFormat.SHORT, getLocale())
| ||||||||||||||||||||||||||||
medium
|
DateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())
| ||||||||||||||||||||||||||||
long
| DateFormat.getDateInstance(DateFormat.LONG, getLocale())
| ||||||||||||||||||||||||||||
full
| DateFormat.getDateInstance(DateFormat.FULL, getLocale())
| ||||||||||||||||||||||||||||
| SubformatPattern | new SimpleDateFormat(subformatPattern, getLocale())
| ||||||||||||||||||||||||||||
MessageFormat msg = messageFormatInstance("There {0, plural, one{is one file}other{are {0} files}} on {1}.")
msg.render(1000, "disk"); // == "There are 1,000 files on disk."
pattern - Format pattern that follows the conventions of
MessageFormatpublic static MessageFormat messageFormatInstance(String pattern, Locale locale)
Same as messageFormatInstance for
the specified locale.
locale - Target localepattern - Format pattern that follows the conventions of
MessageFormatpublic static String naturalDay(Date value)
Same as naturalDay with DateFormat.SHORT
style.
value - Date to be convertedpublic static String naturalDay(Date value, Locale locale)
Same as naturalDay for the specified locale.
value - Date to be convertedlocale - Target localepublic static String naturalDay(int style, Date value)
For dates that are the current day or within one day, return "today", "tomorrow" or "yesterday", as appropriate. Otherwise, returns a string formatted according to a locale sensitive DateFormat.
style - DateFormat style. RELATIVE_SHORT, RELATIVE_MEDIUM or
RELATIVE_LONGvalue - Date to be convertedpublic static String naturalTime(Date duration)
Same as naturalTime with current date as
reference.
duration - Date to be used as duration from current datepublic static String naturalTime(Date reference, Date duration)
Computes both past and future relative dates.
E.g. "1 day ago", "1 day from now", "10 years ago", "3 minutes from now" and so on.
reference - Date to be used as referenceduration - Date to be used as duration from referencepublic static String naturalTime(Date reference, Date duration, Locale locale)
Same as naturalTime for the specified
locale.
reference - Date to be used as referenceduration - Date to be used as duration from referencelocale - Target localepublic static String naturalTime(Date duration, Locale locale)
naturalTime for the specified locale.duration - Date to be used as duration from current datelocale - Target localepublic static String ordinalize(Number value)
Converts a number to its ordinal as a string.
| Input | Output |
|---|---|
| 1 | "1st" |
| 2 | "2nd" |
| 3 | "3rd" |
| 4 | "4th" |
| 1002 | "1002nd" |
| 2012 | "2012th" |
value - Number to be convertedpublic static String ordinalize(Number value, Locale locale)
Same as ordinalize for the specified locale.
value - Number to be convertedlocale - Target localepublic static Number parseNumber(String text) throws ParseException
Converts the given text to number.
text - String containing a spelled out number.ParseExceptionpublic static Number parseNumber(String text, Locale locale) throws ParseException
Same as parseNumber for the specified
locale.
text - String containing a spelled out number.locale - Target localeParseExceptionpublic static String pluralize(Locale locale, String pattern, Number value, Object... args)
Same as pluralize(String, Number, Object...) for the target
locale.
locale - The target localepattern - The formatting pattern with plural rulesvalue - The number that will trigger plural categoryargs - Optional arguments for the formatting patternpublic static String pluralize(String pattern, Number value, Object... args)
Helper method to use ICU message patterns with Language Plural Rules logic. For categories available by language, please see: http://www.unicode.org/cldr/charts/latest/supplemental/ language_plural_rules.html
Examples:
pluralize("Hi {0, plural, one{man}other{men}}!", 1) // == "Hi man!"
pluralize("Hi {0, plural, one{man}other{men}}!", 25) // == "Hi men!"
pattern - The formatting pattern with plural rulesvalue - The number that will trigger plural categoryargs - Optional arguments for the formatting patternpublic static String replaceSupplementary(String value)
Replaces characters outside the Basic Multilingual Plane with their name.
value - The text to be matchedpublic static String smartDateFormat(Date value, String skeleton)
Guesses the best locale-dependent pattern to format the date/time fields that the skeleton specifies.
value - The date to be formattedskeleton - A pattern containing only the variable fields. For example,
"MMMdd" and "mmhh" are skeletons.public static String smartDateFormat(Date value, String skeleton, Locale locale)
Same as smartDateFormat for the specified
locale.
value - The date to be formattedskeleton - A pattern containing only the variable fields. For example,
"MMMdd" and "mmhh" are skeletons.locale - Target localepublic static String spellNumber(Number value)
Converts the given number to words.
| Input | Output |
|---|---|
| 2840 | "two thousand eight hundred and forty" |
| 1412605 | "one million four hundred and twelve thousand six hundred and five" |
| 23380000000L | "twenty-three billion three hundred and eighty million" |
| 90489348043803948043 BigInt | "ninety quintillion four hundred and eighty-nine quadrillion three hundred and forty-eight trillion and forty-three billion eight hundred and three million nine hundred and forty-eight thousand and forty-three" |
value - Number to be convertedpublic static String spellNumber(Number value, Locale locale)
Same as spellNumber for the specified
locale.
value - Number to be convertedlocale - Target localepublic static String transliterate(String text)
text - The text to be transformedpublic static String transliterate(String text, String id)
The simplest identifier is a 'basic ID'.
basicID := (<source> "-")? <target> ("/" <variant>)?
A basic ID typically names a source and target. In "Katakana-Latin", "Katakana" is the source and "Latin" is the target. The source specifier describes the characters or strings that the transform will modify. The target specifier describes the result of the modification. If the source is not given, then the source is "Any", the set of all characters. Source and Target specifiers can be Script IDs (long like "Latin" or short like "Latn"), Unicode language Identifiers (like fr, en_US, or zh_Hant), or special tags (like Any or Hex). For example:
Katakana-Latin Null Hex-Any/Perl Latin-el Greek-en_US/UNGEGN
Some basic IDs contain a further specifier following a forward slash. This is the variant, and it further specifies the transform when several versions of a single transformation are possible. For example, ICU provides several transforms that convert from Unicode characters to escaped representations. These include standard Unicode syntax "U+4E01", Perl syntax "\x{4E01}", XML syntax "丁", and others. The transforms for these operations are named "Any-Hex/Unicode", "Any-Hex/Perl", and "Any-Hex/XML", respectively. If no variant is specified, then the default variant is selected. In the example of "Any-Hex", this is the Java variant (for historical reasons), so "Any-Hex" is equivalent to "Any-Hex/Java".
text - The text to be transformedid - The transliterator identifierCopyright © 2015. All Rights Reserved.