类 ConversionUtils
java.lang.Object
org.apache.velocity.tools.ConversionUtils
Utility methods for parsing or otherwise converting between types.
Current supported types are Number, Date, Calendar,
String, Boolean, Locale and URL
- 作者:
- Nathan Bubna
-
字段概要
字段修饰符和类型字段说明static final ConversionUtils
private static final int
private static final int
private static final int
private static final int
-
构造器概要
构造器 -
方法概要
修饰符和类型方法说明static DateFormat
getDateFormat
(int dateStyle, int timeStyle, Locale locale, TimeZone timezone) static DateFormat
getDateFormat
(String dateStyle, String timeStyle, Locale locale, TimeZone timezone) static DateFormat
getDateFormat
(String format, 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.static NumberFormat
getNumberFormat
(int numberStyle, Locale locale) Returns aNumberFormat
instance for the specified number style andLocale
.static NumberFormat
getNumberFormat
(String format, Locale locale) Returns aNumberFormat
instance for the specified format andLocale
.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
Converts any Object to a boolean usingtoString(Object)
andBoolean.valueOf(String)
.static Calendar
toCalendar
(Date date, Locale locale) static Date
static Date
static Date
static Locale
Converts a string to aLocale
static Number
static Number
Just liketoNumber(Object)
except that you can tell this to attempt parsing the object as a String by passingtrue
as the second parameter.static Number
static Number
static String
Converts objects to String in a more Tools-ish way than String.valueOf(Object), especially with nulls, Arrays and Collections.static String
toString
(Collection values) Returns the first value as a String, if any; otherwise returns null.static URL
Converts a string to aURL
.static URL
Converts a string to aURL
.
-
字段详细资料
-
构造器详细资料
-
ConversionUtils
private ConversionUtils()
-
-
方法详细资料
-
getInstance
-
getNumberFormat
Returns aNumberFormat
instance for the specified format andLocale
. 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 customizedNumberFormat
will be returned.- 参数:
format
- the custom or standard formatting pattern to be usedlocale
- theLocale
to be used- 返回:
- an instance of
NumberFormat
- 另请参阅:
-
getNumberFormat
Returns aNumberFormat
instance for the specified number style andLocale
.- 参数:
numberStyle
- the number style (number will be ignored if this is less than zero or the number style is not recognized)locale
- theLocale
to be used- 返回:
- an instance of
NumberFormat
ornull
if an instance cannot be constructed with the given parameters
-
getNumberStyleAsInt
Checks a string to see if it matches one of the standard NumberFormat style patterns: number, currency, percent, integer, or default. if it does it will return the integer constant for that pattern. if not, it will return -1.- 参数:
style
- the string to be checked- 返回:
- the int identifying the style pattern
- 另请参阅:
-
toNumber
Attempts to convert an unidentifiedObject
into aNumber
, just short of turning it into a string and parsing it. In other words, this will convert toNumber
from aNumber
,Calendar
, orDate
. If it can't do that, it will get the string value and havetoNumber(String,String,Locale)
try to parse it using the default Locale and format.- 参数:
obj
- - the object to convert
-
toNumber
Just liketoNumber(Object)
except that you can tell this to attempt parsing the object as a String by passingtrue
as the second parameter. If you do so, then it will havetoNumber(String,String,Locale)
try to parse it using the default Locale and format. -
toNumber
-
toNumber
Converts an object to an instance ofNumber
using the specified format andLocale
to parse it, if necessary. -
getDateFormat
Returns aDateFormat
instance for the specified format,Locale
, andTimeZone
. 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 customizedSimpleDateFormat
will be returned.- 参数:
format
- the custom or standard formatting pattern to be usedlocale
- theLocale
to be usedtimezone
- theTimeZone
to be used- 返回:
- an instance of
DateFormat
- 另请参阅:
-
getDateFormat
public static DateFormat getDateFormat(String dateStyle, String timeStyle, Locale locale, TimeZone timezone) - 参数:
dateStyle
- the date styletimeStyle
- the time stylelocale
- theLocale
to be usedtimezone
- theTimeZone
to be used- 返回:
- an instance of
DateFormat
- 另请参阅:
-
getDateFormat
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
- theLocale
to be usedtimezone
- theTimeZone
to be used- 返回:
- an instance of
DateFormat
ornull
if an instance cannot be constructed with the given parameters
-
getDateStyleAsInt
Checks a string to see if it matches one of the standard DateFormat style patterns: full, long, medium, short, or default. If it does, it will return the integer constant for that pattern. If not, it will return -1.- 参数:
style
- the string to be checked- 返回:
- the int identifying the style pattern
- 另请参阅:
-
toDate
Attempts to convert an unidentifiedObject
into aDate
, just short of turning it into a string and parsing it. In other words, this will convert toDate
from aDate
,Calendar
, orNumber
. If it can't do that, it will returnnull
.- 参数:
obj
- - the object to convert
-
toDate
Converts an object to an instance ofDate
using the specified format,Locale
, andTimeZone
if the object is not already an instance of Date, Calendar, or Long. -
toDate
Converts an object to an instance ofDate
using the specified format,Locale
, andTimeZone
if the object is not already an instance of Date, Calendar, or Long. -
toCalendar
-
toString
Converts objects to String in a more Tools-ish way than String.valueOf(Object), especially with nulls, Arrays and Collections. Null returns null, Arrays and Collections return their first value, or null if they have no values.- 参数:
value
- the object to be turned into a String- 返回:
- the string value of the object or null if the value is null or it is an array whose first value is null
-
toString
Returns the first value as a String, if any; otherwise returns null.- 参数:
values
- the Collection to be turned into a string- 返回:
- the string value of the first object in the collection or null if the collection is empty
-
toBoolean
Converts any Object to a boolean usingtoString(Object)
andBoolean.valueOf(String)
.- 参数:
value
- the object to be converted- 返回:
- a
Boolean
object for the specified value ornull
if the value is null or the conversion failed
-
toLocale
Converts a string to aLocale
- 参数:
value
- - the string to parse- 返回:
- the
Locale
ornull
if the parsing fails
-
toURL
Converts a string to aURL
. 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 returnnull
.- 参数:
value
- - the string to parse- 返回:
- the
URL
form of the string ornull
- 另请参阅:
-
toURL
Converts a string to aURL
. 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 returnnull
.- 参数:
value
- - the string to parsecaller
- - the object or Class seeking the url- 返回:
- the
URL
form of the string ornull
- 另请参阅:
-