public class ConversionUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static ConversionUtils |
INSTANCE |
| Modifier and Type | Method and Description |
|---|---|
static List |
asList(Object value) |
static DateFormat |
getDateFormat(int dateStyle,
int timeStyle,
Locale locale,
TimeZone timezone)
|
static DateFormat |
getDateFormat(String format,
Locale locale,
TimeZone timezone)
|
static DateFormat |
getDateFormat(String dateStyle,
String timeStyle,
Locale locale,
TimeZone timezone)
|
static int |
getDateStyleAsInt(String style)
Checks a string to see if it matches one of the standard DateFormat
style patterns: full, long, medium, short, or default.
|
ConversionUtils |
getInstance() |
static NumberFormat |
getNumberFormat(int numberStyle,
Locale locale)
Returns a
NumberFormat instance for the specified
number style and Locale. |
static NumberFormat |
getNumberFormat(String format,
Locale locale)
Returns a
NumberFormat instance for the specified
format and Locale. |
static int |
getNumberStyleAsInt(String style)
Checks a string to see if it matches one of the standard
NumberFormat style patterns:
number, currency, percent, integer, or default.
|
static Boolean |
toBoolean(Object value)
Converts any Object to a boolean using
toString(Object)
and Boolean.valueOf(String). |
static Calendar |
toCalendar(Date date,
Locale locale) |
static Date |
toDate(Object obj)
|
static Date |
toDate(Object obj,
String format,
Locale locale,
TimeZone timezone)
|
static Date |
toDate(String str,
String format,
Locale locale,
TimeZone timezone)
|
static Locale |
toLocale(String value)
Converts a string to a
Locale |
static Number |
toNumber(Object obj)
|
static Number |
toNumber(Object obj,
boolean handleStrings)
Just like
toNumber(Object) except that you can tell
this to attempt parsing the object as a String by passing true
as the second parameter. |
static Number |
toNumber(Object value,
String format,
Locale locale)
|
static Number |
toNumber(String value,
String format,
Locale locale)
|
static String |
toString(Collection values)
Returns the first value as a String, if any; otherwise returns null.
|
static String |
toString(Object value)
Converts objects to String in a more Tools-ish way than
String.valueOf(Object), especially with nulls, Arrays and Collections.
|
static URL |
toURL(String value)
Converts a string to a
URL. |
static URL |
toURL(String value,
Object caller)
Converts a string to a
URL. |
public static final ConversionUtils INSTANCE
public ConversionUtils getInstance()
public static NumberFormat getNumberFormat(String format, Locale locale)
NumberFormat instance for the specified
format and Locale. If the format specified is a standard
style pattern, then a number instance
will be returned with the number style set to the
specified style. If it is a custom format, then a customized
NumberFormat will be returned.format - the custom or standard formatting pattern to be usedlocale - the Locale to be usedNumberFormatNumberFormatpublic static NumberFormat getNumberFormat(int numberStyle, Locale locale)
NumberFormat instance for the specified
number style and Locale.numberStyle - the number style (number will be ignored if this is
less than zero or the number style is not recognized)locale - the Locale to be usedNumberFormat or null
if an instance cannot be constructed with the given
parameterspublic static int getNumberStyleAsInt(String style)
style - the string to be checkedNumberFormatpublic static Number toNumber(Object obj)
Object into a Number,
just short of turning it into a string and parsing it. In other words,
this will convert to Number from a Number, Calendar,
or Date. If it can't do that, it will get the string value and have
toNumber(String,String,Locale) try to parse it using the
default Locale and format.obj - - the object to convertpublic static Number toNumber(Object obj, boolean handleStrings)
toNumber(Object) except that you can tell
this to attempt parsing the object as a String by passing true
as the second parameter. If you do so, then it will have
toNumber(String,String,Locale) try to parse it using the
default Locale and format.obj - the target objecthandleStrings - whether to try to convert from a stringpublic static Number toNumber(String value, String format, Locale locale)
value - - the string to convertformat - - the format the number is inlocale - - the LocaleNumber or null if no
conversion is possibleNumberFormat.parse(java.lang.String, java.text.ParsePosition)public static Number toNumber(Object value, String format, Locale locale)
Number using the
specified format and Locale to parse it, if necessary.value - - the object to convertformat - - the format the number is inlocale - - the LocaleNumber or null if no
conversion is possibleNumberFormat.parse(java.lang.String, java.text.ParsePosition)public static DateFormat getDateFormat(String format, Locale locale, TimeZone timezone)
DateFormat instance for the specified
format, Locale, and TimeZone. If the format
specified is a standard style pattern, then a date-time instance
will be returned with both the date and time styles set to the
specified style. If it is a custom format, then a customized
SimpleDateFormat will be returned.format - the custom or standard formatting pattern to be usedlocale - the Locale to be usedtimezone - the TimeZone to be usedDateFormatSimpleDateFormat,
DateFormatpublic static DateFormat getDateFormat(String dateStyle, String timeStyle, Locale locale, TimeZone timezone)
dateStyle - the date styletimeStyle - the time stylelocale - the Locale to be usedtimezone - the TimeZone to be usedDateFormatgetDateFormat(int timeStyle, int dateStyle, Locale locale, TimeZone timezone)public static DateFormat getDateFormat(int dateStyle, int timeStyle, Locale locale, TimeZone timezone)
dateStyle - the date style (date will be ignored if this is
less than zero and the date style is not)timeStyle - the time style (time will be ignored if this is
less than zero and the date style is not)locale - the Locale to be usedtimezone - the TimeZone to be usedDateFormat or null
if an instance cannot be constructed with the given
parameterspublic static int getDateStyleAsInt(String style)
style - the string to be checkedDateFormatpublic static Date toDate(Object obj)
Object into a Date,
just short of turning it into a string and parsing it. In other words,
this will convert to Date from a Date, Calendar,
or Number. If it can't do that, it will return null.obj - - the object to convertpublic static Date toDate(Object obj, String format, Locale locale, TimeZone timezone)
Date using the
specified format, Locale, and TimeZone if the
object is not already an instance of Date, Calendar, or Long.obj - - the date to convertformat - - the format the date is inlocale - - the Localetimezone - - the TimeZoneDate or null if no
conversion is possiblegetDateFormat(java.lang.String, java.util.Locale, java.util.TimeZone),
SimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)public static Date toDate(String str, String format, Locale locale, TimeZone timezone)
Date using the
specified format, Locale, and TimeZone if the
object is not already an instance of Date, Calendar, or Long.str - - the string to parseformat - - the format the date is inlocale - - the Localetimezone - - the TimeZoneDate or null if the
parsing failsgetDateFormat(java.lang.String, java.util.Locale, java.util.TimeZone),
SimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)public static String toString(Object value)
value - the object to be turned into a Stringpublic static String toString(Collection values)
values - the Collection to be turned into a stringpublic static Boolean toBoolean(Object value)
toString(Object)
and Boolean.valueOf(String).value - the object to be convertedBoolean object for the specified value or
null if the value is null or the conversion failedpublic static Locale toLocale(String value)
Localevalue - - the string to parseLocale or null if the
parsing failspublic static URL toURL(String value)
URL. It will first try to
treat the string as a File name, then a classpath resource,
then finally as a literal URL. If none of these work, then
this will return null.value - - the string to parseURL form of the string or nullFile,
ClassUtils.getResource(String,Object),
URLpublic static URL toURL(String value, Object caller)
URL. It will first try to
treat the string as a File name, then a classpath resource,
then finally as a literal URL. If none of these work, then
this will return null.value - - the string to parsecaller - - the object or Class seeking the urlURL form of the string or nullFile,
ClassUtils.getResource(String,Object),
URLCopyright © 2002–2021 The Apache Software Foundation. All rights reserved.