public final class Humanize 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 Standard i18n Java APIs.
| Modifier and Type | Method and Description |
|---|---|
static String |
binaryPrefix(Number value)
Converts a given number to a string preceded by the corresponding binary
International System of Units (SI) prefix.
|
static String |
binaryPrefix(Number value,
Locale locale)
Same as
binaryPrefix(Number) for the specified locale. |
static String |
camelize(String text)
Same as
camelize(String, boolean) with capitalize to false. |
static String |
camelize(String text,
boolean capitalizeFirstChar)
Makes a phrase camel case.
|
static String |
camelize(String text,
boolean capitalizeFirstChar,
Locale locale)
Same as
camelize(String, boolean) for the specified locale. |
static String |
camelize(String text,
Locale locale)
Same as
camelize(String) for the specified locale. |
static String |
capitalize(String text)
Capitalizes the given text smartly.
|
static String |
capitalize(String text,
Locale locale)
Same as
capitalize(String) for the specified locale. |
static DateFormat |
dateFormat(String pattern)
Returns a SimpleDateFormat instance for the current thread.
|
static DateFormat |
dateFormat(String pattern,
Locale locale)
Same as
dateFormat(String) for the specified locale. |
static String |
decamelize(String words)
Same as
decamelize(String String) defaulting to SPACE for
replacement
|
static String |
decamelize(String words,
String replacement)
Converts a camel case string into a human-readable name.
|
static DecimalFormat |
decimalFormat(String pattern)
Returns a DecimalFormat instance for the current thread.
|
static DecimalFormat |
decimalFormat(String pattern,
Locale locale)
Same as
decimalFormat(String) for the specified locale. |
static String |
duration(Number seconds)
Formats a number of seconds as hours, minutes and seconds.
|
static String |
duration(Number seconds,
Constants.TimeStyle style)
Formats a number of seconds as hours, minutes and seconds.
|
static String |
duration(Number seconds,
Constants.TimeStyle style,
Locale locale)
Same as
#duration(Number, TimeStyle) for the specified locale. |
static String |
duration(Number seconds,
Locale locale)
Same as
duration(Number) for the specified locale. |
static String |
fixLength(String text,
int charsNum,
char paddingChar)
Pads or truncates a string to a specified length.
|
static String |
fixLength(String text,
int charsNum,
char paddingChar,
boolean left)
Pads or truncates a string to a specified length.
|
static String |
format(Locale locale,
String pattern,
Object... args)
Same as
format(String, Object...) for the specified locale. |
static String |
format(String pattern,
Object... args)
Gets an ExtendedMessageFormat instance for the current thread with the
given pattern and uses it to format a message with the specified
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(Number) for the specified locale. |
static String |
formatDate(Date value)
Same as
formatDate(int, Date) with SHORT style. |
static String |
formatDate(Date value,
Locale locale)
Same as
formatDate(Date) 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(Date, String) 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(int, Date) 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(Date) 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(int, int, Date) 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(Number) 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(Number) for the specified locale. |
static boolean |
lossyEquals(Locale locale,
String source,
String target)
Same as
lossyEquals(String, String) for the specified locale. |
static boolean |
lossyEquals(String source,
String target)
Locale-sensitive string comparison for primary differences.
|
static String |
mask(String mask,
String value)
Formats the given text with the mask specified.
|
static MaskFormat |
maskFormat(String mask)
Returns a
MaskFormat instance for the current thread. |
static MessageFormat |
messageFormat(String pattern)
Returns a MessageFormat instance for the current thread.
|
static MessageFormat |
messageFormat(String pattern,
Locale locale)
Same as
messageFormat(String) for the specified locale. |
static String |
metricPrefix(Number value)
Converts a given number to a string preceded by the corresponding decimal
multiplicative prefix.
|
static String |
metricPrefix(Number value,
Locale locale)
Same as
metricPrefix(Number) for the specified locale. |
static String |
nanoTime(Number value)
Formats a number of nanoseconds as the proper ten power unit.
|
static String |
nanoTime(Number value,
Locale locale)
Same as
nanoTime(Number) for the specified locale. |
static String |
naturalDay(Date then)
Same as
naturalDay(int, Date) with DateFormat.SHORT style. |
static String |
naturalDay(Date then,
Locale locale)
Same as
naturalDay(Date) with the given locale. |
static String |
naturalDay(int style,
Date then)
For dates that are the current day or within one day, return 'today',
'tomorrow' or 'yesterday', as appropriate.
|
static String |
naturalDay(int style,
Date then,
Locale locale)
Same as
naturalDay(int, Date) with the given locale. |
static String |
naturalTime(Date duration)
Same as
naturalTime(Date, Date) 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(Date, Date) for the specified locale. |
static String |
naturalTime(Date reference,
Date duration,
long precision)
Computes both past and future relative dates with arbitrary precision.
|
static String |
naturalTime(Date reference,
Date duration,
long precision,
Locale locale)
Same as
naturalTime(Date, Date, long) for the specified locale. |
static String |
naturalTime(Date reference,
Date duration,
TimeMillis precision)
Same as
naturalTime(Date, Date, long). |
static String |
naturalTime(Date reference,
Date duration,
TimeMillis precision,
Locale locale)
|
static String |
naturalTime(Date duration,
Locale locale)
Same as
naturalTime(Date) for the specified locale. |
static String |
naturalTime(Date duration,
long precision)
Same as
naturalTime(Date, Date, long) with current date as
reference. |
static String |
naturalTime(Date duration,
long precision,
Locale locale)
Same as
naturalTime(Date, long) for the specified locale. |
static String |
naturalTime(Date duration,
TimeMillis precision)
Same as
naturalTime(Date, long). |
static String |
naturalTime(Date duration,
TimeMillis precision,
Locale locale)
Same as
naturalTime(Date, long, Locale). |
static String |
ordinal(Number value)
Converts a number to its ordinal as a string.
|
static String |
ordinal(Number value,
Locale locale)
Same as
ordinal(Number) for the specified locale. |
static String |
oxford(Collection<?> items)
Converts a list of items to a human readable string.
|
static String |
oxford(Collection<?> items,
int limit,
String limitStr)
Converts a list of items to a human readable string with an optional
limit.
|
static String |
oxford(Collection<?> items,
Locale locale)
Same as
oxford(Object[]) for the specified locale. |
static String |
oxford(Object[] items)
Converts a list of items to a human readable string.
|
static String |
oxford(Object[] items,
int limit,
String limitStr)
Converts a list of items to a human readable string with an optional
limit.
|
static String |
oxford(Object[] items,
int limit,
String limitStr,
Locale locale)
Same as
oxford(Object[], int, String) for the specified locale. |
static String |
oxford(Object[] items,
Locale locale)
Same as
oxford(Object[]) for the specified locale. |
static Pace |
pace(Number value,
long interval)
Matches a pace (value and interval) with a logical time frame.
|
static Pace |
pace(Number value,
TimeMillis interval)
Same as
pace(Number, long). |
static String |
paceFormat(Locale locale,
Number value,
long interval)
Same as
paceFormat(Number, long) for a target locale. |
static String |
paceFormat(Locale locale,
Number value,
Parameters.PaceParameters params)
Same as
#paceFormat(Number, long, String, String, String) for a
target locale. |
static String |
paceFormat(Number value,
long interval)
Matches a pace (value and interval) with a logical time frame.
|
static String |
paceFormat(Number value,
Parameters.PaceParameters params)
Matches a pace (value and interval) with a logical time frame.
|
static String |
paceFormat(Number value,
TimeMillis interval)
Same as
paceFormat(Number, long) |
static byte[] |
parseBase64(String base64str)
Converts the string argument into an array of bytes.
|
static Date |
parseISODate(String dateStr)
Converts the string argumento into a Date value.
|
static Date |
parseISODateTime(String dateStr)
Converts the string argumento into a Date value.
|
static Date |
parseISOTime(String timeStr)
Converts the string argumento into a Date value.
|
static Date |
parseSmartDate(String dateStr,
String... fmts)
|
static Date |
parseSmartDateWithSeparator(String dateStr,
String separator,
String... fmts)
Tries to parse a date string applying an array of non lenient format
patterns.
|
static String |
pluralize(Locale locale,
Number number,
Parameters.PluralizeParams params)
Same as
#pluralize(Number, PluralizeParams) for the target
locale. |
static String |
pluralize(Number number,
Parameters.PluralizeParams p)
Applies the proper format for a given plural state.
|
static String |
pluralize(String one,
String many,
String none,
Number number,
Object... exts)
Signature for the main use case of
#pluralize(Number, PluralizeParams). |
static MessageFormat |
pluralizeFormat(String template)
Constructs a message with pluralization logic from the given template.
|
static MessageFormat |
pluralizeFormat(String template,
Locale locale)
Same as
pluralizeFormat(String) for the specified locale. |
static MessageFormat |
pluralizeFormat(String pattern,
String... choices)
Constructs a message with pluralization logic by the means of
ChoiceFormat.
|
static PrettyTimeFormat |
prettyTimeFormat()
Returns a thread-safe
PrettyTimeFormat instance. |
static PrettyTimeFormat |
prettyTimeFormat(Locale locale)
Same as
prettyTimeFormat() for the specified locale. |
static String |
replaceSupplementary(String value)
Replaces characters outside the Basic Multilingual Plane with their name.
|
static Number |
roundToSignificantFigures(Number num,
int precision)
Rounds a number to significant figures.
|
static String |
simplify(String text)
Sort of poor man's transliteration, i.e. normalizes and strips
diacritical marks.
|
static String |
slugify(String text) |
static String |
slugify(String text,
Parameters.SlugifyParams params)
Transforms a text into a representation suitable to be used in an URL.
|
static String |
spellBigNumber(Number value)
Converts a big number to a friendly text representation.
|
static String |
spellBigNumber(Number value,
Locale locale)
Same as
spellBigNumber(Number) for the specified locale. |
static String |
spellDigit(Number value)
For decimal digits [0-9], returns the number spelled out.
|
static String |
spellDigit(Number value,
Locale locale)
Same as
spellDigit(Number) for the specified locale. |
static String |
times(Number num)
Interprets numbers as occurrences.
|
static String |
times(Number num,
Locale locale)
Same as
times(Number) for the specified locale. |
static String |
titleize(String text)
Converts the given text to title case smartly.
|
static String |
titleize(String text,
String[] intCaps)
Converts the given text to title case smartly.
|
static String |
underscore(String text)
Makes a phrase underscored instead of spaced.
|
static String |
unidecode(String text)
Simple, just enough, unicode to ascii transliteration.
|
static String |
unmask(String mask,
String value)
Parses the given text with the mask specified.
|
static String |
wordWrap(String value,
int len)
Truncate a string to the closest word boundary after a number of
characters.
|
public static String binaryPrefix(Number value)
Converts a given number to a string preceded by the corresponding binary International System of Units (SI) prefix.
| Input | Output |
|---|---|
| 2 | "2 bytes" |
| 1536 | "1.5 KB" |
| 5242880 | "5.00 MB" |
| 1325899906842624L | "1.18 PB" |
value - Number to be convertedpublic static String binaryPrefix(Number value, Locale locale)
Same as binaryPrefix(Number) for the specified locale.
value - Number to be convertedlocale - Target localepublic static String camelize(String text)
Same as camelize(String, boolean) with capitalize to false.
text - String to be camelizedpublic static String camelize(String text, boolean capitalizeFirstChar)
Makes a phrase camel case. Spaces, hyphens, underscores and dots will be removed.
capitalizeFirstChar - true makes the first letter uppercasetext - String to be camelizedpublic static String camelize(String text, boolean capitalizeFirstChar, Locale locale)
Same as camelize(String, boolean) for the specified locale.
capitalizeFirstChar - true makes the first letter uppercasetext - String to be camelizedpublic static String camelize(String text, Locale locale)
Same as camelize(String) for the specified locale.
text - String to be camelizedlocale - Target localepublic static String capitalize(String text)
Capitalizes the given text smartly.
text - String to be capitalizedpublic static String capitalize(String text, Locale locale)
Same as capitalize(String) for the specified locale.
text - String to be capitalizedlocale - Target localepublic static DateFormat dateFormat(String pattern)
Returns a SimpleDateFormat instance for the current thread.
Date and time formats are specified by date and time pattern
strings. Within date and time pattern strings, unquoted letters from
'A' to 'Z' and from 'a' to
'z' are interpreted as pattern letters representing the
components of a date or time string. Text can be quoted using single
quotes (') to avoid interpretation. "''"
represents a single quote. All other characters are not interpreted;
they're simply copied into the output string during formatting or matched
against the input string during parsing.
The following pattern letters are defined (all other characters from
'A' to 'Z' and from 'a' to
'z' are reserved):
Pattern letters are usually repeated, as their number determines the exact presentation:
Letter Date or Time Component Presentation Examples GEra designator Text ADyYear Year 1996;96MMonth in year Month July;Jul;07wWeek in year Number 27WWeek in month Number 2DDay in year Number 189dDay in month Number 10FDay of week in month Number 2EDay in week Text Tuesday;TueaAm/pm marker Text PMHHour in day (0-23) Number 0kHour in day (1-24) Number 24KHour in am/pm (0-11) Number 0hHour in am/pm (1-12) Number 12mMinute in hour Number 30sSecond in minute Number 55SMillisecond Number 978zTime zone General time zone Pacific Standard Time;PST;GMT-08:00ZTime zone RFC 822 time zone -0800
Calendar is the Gregorian calendar, the following
rules are applied.SimpleDateFormat must interpret the abbreviated year
relative to some century. It does this by adjusting dates to be within 80
years before and 20 years after the time the
SimpleDateFormat instance is created. For example, using a
pattern of "MM/dd/yy" and a SimpleDateFormat instance
created on Jan 1, 1997, the string "01/11/12" would be interpreted as Jan
11, 2012 while the string "05/04/64" would be interpreted as May 4, 1964.
During parsing, only strings consisting of exactly two digits, as defined
by Character.isDigit(char), will be parsed into the default
century. Any other numeric string, such as a one digit string, a three or
more digit string, or a two digit string that isn't all digits (for
example, "-1"), is interpreted literally. So "01/02/3" or "01/02/003" are
parsed, using the same pattern, as Jan 2, 3 AD. Likewise, "01/02/-3" is
parsed as Jan 2, 4 BC.
GMTOffsetTimeZone:
GMT Sign Hours : Minutes
Sign: one of
+ -
Hours:
Digit
Digit Digit
Minutes:
Digit Digit
Digit: one of
0 1 2 3 4 5 6 7 8 9
Hours must be between 0 and 23, and Minutes must be between
00 and 59. The format is locale independent and digits must be taken from
the Basic Latin block of the Unicode standard.
For parsing, RFC 822 time zones are also accepted.
RFC822TimeZone:
Sign TwoDigitHours Minutes
TwoDigitHours:
Digit Digit
TwoDigitHours must be between 00 and 23. Other definitions are as
for general time zones.
For parsing, general time zones are also accepted.
SimpleDateFormat also supports localized date and time
pattern strings. In these strings, the pattern letters described
above may be replaced with other, locale dependent, pattern letters.
SimpleDateFormat does not deal with the localization of text
other than the pattern letters; that's up to the client of the class.
Date and Time Pattern Result "yyyy.MM.dd G 'at' HH:mm:ss z"2001.07.04 AD at 12:08:56 PDT"EEE, MMM d, ''yy"Wed, Jul 4, '01"h:mm a"12:08 PM"hh 'o''clock' a, zzzz"12 o'clock PM, Pacific Daylight Time"K:mm a, z"0:08 PM, PDT"yyyyy.MMMMM.dd GGG hh:mm aaa"02001.July.04 AD 12:08 PM"EEE, d MMM yyyy HH:mm:ss Z"Wed, 4 Jul 2001 12:08:56 -0700"yyMMddHHmmssZ"010704120856-0700"yyyy-MM-dd'T'HH:mm:ss.SSSZ"2001-07-04T12:08:56.235-0700
pattern - Format pattern that follows the conventions of
SimpleDateFormatpublic static DateFormat dateFormat(String pattern, Locale locale)
Same as dateFormat(String) for the specified locale.
pattern - Format pattern that follows the conventions of
SimpleDateFormatlocale - Target localepublic static String decamelize(String words)
Same as decamelize(String String) defaulting to SPACE for
replacement
words - String to be convertedpublic static String decamelize(String words, String replacement)
Converts a camel case string into a human-readable name.
Example assuming SPACE as replacement:
| Input | Output |
|---|---|
| "MyClass" | "My Class" |
| "GL11Version" | "GL 11 Version" |
| "AString" | "A String" |
| "SimpleXMLParser" | "Simple XML Parser" |
words - String to be convertedreplacement - String to be interpolatedpublic static DecimalFormat decimalFormat(String pattern)
Returns a DecimalFormat instance for the current thread.
pattern - Format pattern that follows the conventions of
DecimalFormatpublic static DecimalFormat decimalFormat(String pattern, Locale locale)
Same as decimalFormat(String) for the specified locale.
pattern - Format pattern that follows the conventions of
DecimalFormatlocale - Target localepublic static String duration(Number seconds)
Formats a number of seconds as hours, minutes and seconds. Defaults to STANDARD time style.
seconds - Number of secondspublic static String duration(Number seconds, Locale locale)
Same as duration(Number) for the specified locale.
seconds - Number of secondslocale - Target localepublic static String duration(Number seconds, Constants.TimeStyle style)
Formats a number of seconds as hours, minutes and seconds.
seconds - Number of secondsstyle - Time stylepublic static String duration(Number seconds, Constants.TimeStyle style, Locale locale)
Same as #duration(Number, TimeStyle) for the specified locale.
seconds - Number of secondsstyle - Time stylelocale - Target localepublic static String fixLength(String text, int charsNum, char paddingChar)
Pads or truncates a string to a specified length.
text - String to be fixedcharsNum - The fixed length in number of charspaddingChar - The padding characterpublic static String fixLength(String text, int charsNum, char paddingChar, boolean left)
Pads or truncates a string to a specified length.
text - String to be fixedcharsNum - The fixed length in number of charspaddingChar - The padding characterleft - true for left paddingpublic static String format(Locale locale, String pattern, Object... args)
Same as format(String, Object...) for the specified locale.
locale - The target localepattern - Format pattern that follows the conventions of
MessageFormatargs - Argumentspublic static String format(String pattern, Object... args)
Gets an ExtendedMessageFormat instance for the current thread with the given pattern and uses it to format a message with the specified 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(Number) for the specified locale.
value - Number to be formattedlocale - Target localepublic static String formatDate(Date value)
Same as formatDate(int, Date) with SHORT style.
value - Date to be formattedpublic static String formatDate(Date value, Locale locale)
Same as formatDate(Date) 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.dateFormat(String)public static String formatDate(Date value, String pattern, Locale locale)
Same as formatDate(Date, String) for the specified locale.
value - Date to be formattedpattern - The pattern.locale - Target localedateFormat(String)public 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(int, Date) 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(Date) 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(int, int, Date) 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(Number) 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(Number) for the specified locale.
value - Ratio to be convertedlocale - Target localepublic static boolean lossyEquals(Locale locale, String source, String target)
Same as lossyEquals(String, String) for the specified locale.
locale - The target localesource - The source string to be comparedtarget - The target string to be comparedpublic static boolean lossyEquals(String source, String target)
Locale-sensitive string comparison for primary differences.
loosyEquals("Aáà-aa", "aaaaa") // == true
loosyEquals("abc", "cba") // == false
source - The source string to be comparedtarget - The target string to be comparedpublic static String mask(String mask, String value)
Formats the given text with the mask specified.
mask - The pattern mask.value - The text to be maskedMaskFormatpublic static MaskFormat maskFormat(String mask)
Returns a MaskFormat instance for the current thread.
mask - The pattern maskMaskFormat instancepublic static MessageFormat messageFormat(String pattern)
Returns a MessageFormat instance for the current thread.
pattern - Format pattern that follows the conventions of
MessageFormatpublic static MessageFormat messageFormat(String pattern, Locale locale)
Same as messageFormat(String) for the specified locale.
locale - Target localepattern - Format pattern that follows the conventions of
MessageFormatpublic static String metricPrefix(Number value)
Converts a given number to a string preceded by the corresponding decimal multiplicative prefix.
| Input | Output |
|---|---|
| 200 | "200" |
| 1000 | "1k" |
| 3500000 | "3.5M" |
value - Number to be convertedpublic static String metricPrefix(Number value, Locale locale)
Same as metricPrefix(Number) for the specified locale.
value - Number to be convertedlocale - Target localepublic static String nanoTime(Number value)
Formats a number of nanoseconds as the proper ten power unit.
value - Number of nanosecondspublic static String nanoTime(Number value, Locale locale)
Same as nanoTime(Number) for the specified locale.
value - Number of nanosecondslocale - Target localepublic static String naturalDay(Date then)
naturalDay(int, Date) with DateFormat.SHORT style.then - The datepublic static String naturalDay(Date then, Locale locale)
naturalDay(Date) with the given locale.then - The datelocale - Target localepublic static String naturalDay(int style, Date then)
style - The style of the Datethen - The date (GMT)public static String naturalDay(int style, Date then, Locale locale)
naturalDay(int, Date) with the given locale.style - The style of the Datethen - The date (GMT)locale - Target localepublic static String naturalTime(Date duration)
naturalTime(Date, Date) with current date as reference.duration - The durationpublic static String naturalTime(Date reference, Date duration)
E.g. 'one day ago', 'one day from now', '10 years ago', '3 minutes from now', 'right now' and so on.
reference - The referenceduration - The durationpublic static String naturalTime(Date reference, Date duration, Locale locale)
naturalTime(Date, Date) for the specified locale.reference - The referenceduration - The durationlocale - The localepublic static String naturalTime(Date reference, Date duration, long precision)
duration - The durationprecision - The precision to retain in millisecondspublic static String naturalTime(Date reference, Date duration, long precision, Locale locale)
naturalTime(Date, Date, long) for the specified locale.reference - The referenceduration - The durationprecision - The precesion to retain in millisecondslocale - The localepublic static String naturalTime(Date reference, Date duration, TimeMillis precision)
naturalTime(Date, Date, long).duration - The durationprecision - The precision to retain in millisecondspublic static String naturalTime(Date reference, Date duration, TimeMillis precision, Locale locale)
reference - The referenceduration - The durationprecision - The precesion to retain in millisecondslocale - The localepublic static String naturalTime(Date duration, Locale locale)
naturalTime(Date) for the specified locale.duration - The durationlocale - The localepublic static String naturalTime(Date duration, long precision)
naturalTime(Date, Date, long) with current date as
reference.duration - The durationprecision - The precision to retain in millisecondspublic static String naturalTime(Date duration, long precision, Locale locale)
naturalTime(Date, long) for the specified locale.duration - The durationprecision - The precesion to retain in millisecondslocale - The localepublic static String naturalTime(Date duration, TimeMillis precision)
naturalTime(Date, long).duration - The durationprecision - The precision to retain in millisecondspublic static String naturalTime(Date duration, TimeMillis precision, Locale locale)
naturalTime(Date, long, Locale).duration - The durationprecision - The precesion to retain in millisecondslocale - The localepublic static String ordinal(Number value)
E.g. 1 becomes '1st', 2 becomes '2nd', 3 becomes '3rd', etc.
value - The number to convertpublic static String ordinal(Number value, Locale locale)
ordinal(Number) for the specified locale.value - The number to convertlocale - The localepublic static String oxford(Collection<?> items)
Converts a list of items to a human readable string.
items - The items collectionpublic static String oxford(Collection<?> items, int limit, String limitStr)
Converts a list of items to a human readable string with an optional limit.
items - The items collectionlimit - The number of items to print. -1 for unbounded.limitStr - The string to be appended after extra items. Null or "" for
default.public static String oxford(Collection<?> items, Locale locale)
oxford(Object[]) for the specified locale.items - The items collectionlocale - Target localepublic static String oxford(Object[] items)
Converts a list of items to a human readable string.
items - The items arraypublic static String oxford(Object[] items, int limit, String limitStr)
Converts a list of items to a human readable string with an optional limit.
items - The items arraylimit - The number of items to print. -1 for unbounded.limitStr - The string to be appended after extra items. Null or "" for
default.public static String oxford(Object[] items, int limit, String limitStr, Locale locale)
oxford(Object[], int, String) for the specified locale.items - The items arraylimit - The number of items to print. -1 for unbounded.limitStr - The string to be appended after extra items. Null or "" for
default.locale - Target localepublic static String oxford(Object[] items, Locale locale)
oxford(Object[]) for the specified locale.items - The items arraylocale - Target localepublic static Pace pace(Number value, long interval)
Examples:
// 3 occurrences within a 3000ms interval
pace(3, 3000); // => ~1/sec.
// 200 occurrences within a 70000ms interval
pace(200, 70000); // => ~3/sec.
// 10 occurrences within a 70000ms interval
pace(10, 70000); // => ~9/min.
// 14 occurrences within a 31557600000ms interval (a year)
pace(14, 31557600000L); // => ~1/month
// 25 occurrences within a 31557600000ms interval
pace(25, 31557600000L); // => ~2/month
// 9 occurrences within a 31557600000ms interval
pace(9, 31557600000L); // => >1/month (less than one per month)
value - The number of occurrences within the specified intervalinterval - The interval in millisecondspublic static Pace pace(Number value, TimeMillis interval)
pace(Number, long).value - The number of occurrences within the specified intervalinterval - Time millis intervalpublic static String paceFormat(Locale locale, Number value, long interval)
paceFormat(Number, long) for a target locale.locale - The target localevalue - The number of occurrences within the specified intervalinterval - The interval in millisecondspublic static String paceFormat(Locale locale, Number value, Parameters.PaceParameters params)
#paceFormat(Number, long, String, String, String) for a
target locale.locale - The target localevalue - The number of occurrences within the specified intervalparams - The pace format parameterezationpublic static String paceFormat(Number value, long interval)
value - The number of occurrences within the specified intervalinterval - The interval in millisecondspublic static String paceFormat(Number value, Parameters.PaceParameters params)
value - The number of occurrences within the specified intervalparams - The pace format parameterizationpublic static String paceFormat(Number value, TimeMillis interval)
paceFormat(Number, long)value - The number of occurrences within the specified intervalinterval - The interval in millisecondspublic static byte[] parseBase64(String base64str)
base64str - The Base64 encoded stringpublic static Date parseISODate(String dateStr)
dateStr - The ISO8601 date stringpublic static Date parseISODateTime(String dateStr)
dateStr - The ISO8601 date stringpublic static Date parseISOTime(String timeStr)
timeStr - The ISO8601 time stringpublic static Date parseSmartDate(String dateStr, String... fmts)
Same as parseSmartDateWithSeparator(String, String, String...)
but with "[\\D-_\\s]+" as the default separator.
Date target = newDate(2012, 1, 1, 0, 0, 0);
String dates[] = new String[] { "1.2.12", "01.02.2012", "2012.02.01", "01-02-12", "1 2 2012" };
for (String ds : dates)
{
Date date = Humanize.parseSmartDate(ds, "dd/MM/yy", "yyyy/MM/dd", "dd/MM/yyyy");
Assert.assertEquals(date, target);
}
dateStr - The date stringfmts - An array of formatspublic static Date parseSmartDateWithSeparator(String dateStr, String separator, String... fmts)
Tries to parse a date string applying an array of non lenient format patterns. The formats are automatically cached.
dateStr - The date stringseparator - The separator regexpfmts - An array of formatsparseSmartDate(String, String...)public static String pluralize(Locale locale, Number number, Parameters.PluralizeParams params)
Same as #pluralize(Number, PluralizeParams) for the target
locale.
locale - Target localenumber - The number that triggers the plural statepublic static String pluralize(Number number, Parameters.PluralizeParams p)
Applies the proper format for a given plural state.
Example:
PluralizeParams p = PluralizeParams
.begin("There is one file on {1}.")
.many("There are {0} files on {1}.")
.none("There are no files on {1}.")
.exts("disk");
pluralize(1, p);
// == There is one file on disk.
pluralize(2, p);
// == There are 2 files on disk.
pluralize(0, p);
// == There are no files on disk.
// ---
PluralizeParams p = PluralizeParams
.begin("one")
.many("{0}")
.none("none");
pluralize(1, p);
// = "one"
pluralize(2, p);
// = "2"
number - The number that triggers the plural statepublic static String pluralize(String one, String many, String none, Number number, Object... exts)
#pluralize(Number, PluralizeParams).one - Format for a single elementmany - Format for many elementsnone - Format for no elementnumber - The number that triggers the plural stateOptional - extension objects to be passed to the formatterpublic static MessageFormat pluralizeFormat(String template)
Constructs a message with pluralization logic from the given template.
MessageFormat msg = pluralize("There {0} on {1}.::are no files::is one file::are {2} files");
msg.render(0, "disk"); // == "There are no files on disk."
msg.render(1, "disk"); // == "There is one file on disk."
msg.render(1000, "disk"); // == "There are 1,000 files on disk."
MessageFormat msg = pluralize("nothing::one thing::{0} things");
msg.render(-1); // == "nothing"
msg.render(0); // == "nothing"
msg.render(1); // == "one thing"
msg.render(2); // == "2 things"
MessageFormat msg = pluralize("one thing::{0} things");
msg.render(-1); // == "-1 things"
msg.render(0); // == "0 things"
msg.render(1); // == "one thing"
msg.render(2); // == "2 things"
template - String of tokens delimited by '::'public static MessageFormat pluralizeFormat(String template, Locale locale)
Same as pluralizeFormat(String) for the specified locale.
template - String of tokens delimited by '::'locale - Target localepublic static MessageFormat pluralizeFormat(String pattern, String... choices)
Constructs a message with pluralization logic by the means of ChoiceFormat.
pattern - Base patternchoices - Values that match the patternpublic static PrettyTimeFormat prettyTimeFormat()
Returns a thread-safe PrettyTimeFormat instance.
PrettyTimeFormatpublic static PrettyTimeFormat prettyTimeFormat(Locale locale)
Same as prettyTimeFormat() for the specified locale.
locale - Target localePrettyTimeFormatpublic static String replaceSupplementary(String value)
Replaces characters outside the Basic Multilingual Plane with their name.
value - The text to be matchedpublic static Number roundToSignificantFigures(Number num, int precision)
Rounds a number to significant figures.
num - The number to be roundedprecision - The number of significant digitspublic static String simplify(String text)
Sort of poor man's transliteration, i.e. normalizes and strips diacritical marks.
| Input | Output |
|---|---|
| "J'étudie le français" | "J'etudie le francais" |
| "Lo siento, no hablo español." | "Lo siento, no hablo espanol." |
text - The text to be simplified.public static String slugify(String text)
text - The text to be slugifiedpublic static String slugify(String text, Parameters.SlugifyParams params)
Transforms a text into a representation suitable to be used in an URL.
| Input | Output |
|---|---|
| "J'étudie le français" | "j-etudie-le-francais" |
| "Lo siento, no hablo español" | "lo-siento-no-hablo-espanol" |
| "\nsome@mail.com\n" | "some-mail-com" |
text - The text to be slugifiedparams - The slugify parameterization objectpublic static String spellBigNumber(Number value)
Converts a big number to a friendly text representation. Accepts values ranging from thousands to googols. Uses BigDecimal.
value - Number to be convertedpublic static String spellBigNumber(Number value, Locale locale)
Same as spellBigNumber(Number) for the specified locale.
value - Number to be convertedlocale - Target localepublic static String spellDigit(Number value)
For decimal digits [0-9], returns the number spelled out. Otherwise, returns the number as string.
| Input | Output |
|---|---|
| 1 | "one" |
| 2 | "two" |
| 10 | "10" |
value - Decimal digitpublic static String spellDigit(Number value, Locale locale)
Same as spellDigit(Number) for the specified locale.
value - Decimal digitlocale - Target localepublic static String times(Number num)
num - The number of occurrencespublic static String times(Number num, Locale locale)
times(Number) for the specified locale.num - The number of occurrenceslocale - Target localepublic static String titleize(String text)
Converts the given text to title case smartly.
Known limitations:text - Text to be convertedpublic static String titleize(String text, String[] intCaps)
Converts the given text to title case smartly.
text - Text to be convertedintCaps - An internal capitalized word listpublic static String underscore(String text)
Makes a phrase underscored instead of spaced.
text - Phrase to underscorepublic static String unidecode(String text)
text - The text to be decodedpublic static String unmask(String mask, String value) throws ParseException
Parses the given text with the mask specified.
mask - The pattern mask.value - The text to be parsedParseExceptionMaskFormatCopyright © 2015. All Rights Reserved.