Package org.apache.velocity.tools
Class ConversionUtils
- java.lang.Object
-
- org.apache.velocity.tools.ConversionUtils
-
public class ConversionUtils extends java.lang.Object
Utility methods for parsing or otherwise converting between types. Current supported types are Number, Date, Calendar, String, Boolean, Locale and URL- Author:
- Nathan Bubna
-
-
Field Summary
Fields Modifier and Type Field Description static ConversionUtils
INSTANCE
private static int
STYLE_CURRENCY
private static int
STYLE_INTEGER
private static int
STYLE_NUMBER
private static int
STYLE_PERCENT
-
Constructor Summary
Constructors Modifier Constructor Description private
ConversionUtils()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.text.DateFormat
getDateFormat(int dateStyle, int timeStyle, java.util.Locale locale, java.util.TimeZone timezone)
Returns aDateFormat
instance for the specified time style, date style,Locale
, andTimeZone
.static java.text.DateFormat
getDateFormat(java.lang.String dateStyle, java.lang.String timeStyle, java.util.Locale locale, java.util.TimeZone timezone)
Returns aDateFormat
instance for the specified date style, time style,Locale
, andTimeZone
.static java.text.DateFormat
getDateFormat(java.lang.String format, java.util.Locale locale, java.util.TimeZone timezone)
Returns aDateFormat
instance for the specified format,Locale
, andTimeZone
.static int
getDateStyleAsInt(java.lang.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 java.text.NumberFormat
getNumberFormat(int numberStyle, java.util.Locale locale)
Returns aNumberFormat
instance for the specified number style andLocale
.static java.text.NumberFormat
getNumberFormat(java.lang.String format, java.util.Locale locale)
Returns aNumberFormat
instance for the specified format andLocale
.static int
getNumberStyleAsInt(java.lang.String style)
Checks a string to see if it matches one of the standard NumberFormat style patterns: number, currency, percent, integer, or default.static java.lang.Boolean
toBoolean(java.lang.Object value)
Converts any Object to a boolean usingtoString(Object)
andBoolean.valueOf(String)
.static java.util.Calendar
toCalendar(java.util.Date date, java.util.Locale locale)
static java.util.Date
toDate(java.lang.Object obj)
Attempts to convert an unidentifiedObject
into aDate
, just short of turning it into a string and parsing it.static java.util.Date
toDate(java.lang.Object obj, java.lang.String format, java.util.Locale locale, java.util.TimeZone timezone)
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.static java.util.Date
toDate(java.lang.String str, java.lang.String format, java.util.Locale locale, java.util.TimeZone timezone)
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.static java.util.Locale
toLocale(java.lang.String value)
Converts a string to aLocale
static java.lang.Number
toNumber(java.lang.Object obj)
Attempts to convert an unidentifiedObject
into aNumber
, just short of turning it into a string and parsing it.static java.lang.Number
toNumber(java.lang.Object obj, boolean handleStrings)
Just liketoNumber(Object)
except that you can tell this to attempt parsing the object as a String by passingtrue
as the second parameter.static java.lang.Number
toNumber(java.lang.Object value, java.lang.String format, java.util.Locale locale)
Converts an object to an instance ofNumber
using the specified format andLocale
to parse it, if necessary.static java.lang.Number
toNumber(java.lang.String value, java.lang.String format, java.util.Locale locale)
Converts a string to an instance ofNumber
using the specified format andLocale
to parse it.static java.lang.String
toString(java.lang.Object value)
Converts objects to String in a more Tools-ish way than String.valueOf(Object), especially with nulls, Arrays and Collections.static java.lang.String
toString(java.util.Collection values)
Returns the first value as a String, if any; otherwise returns null.static java.net.URL
toURL(java.lang.String value)
Converts a string to aURL
.static java.net.URL
toURL(java.lang.String value, java.lang.Object caller)
Converts a string to aURL
.
-
-
-
Field Detail
-
INSTANCE
public static final ConversionUtils INSTANCE
-
STYLE_NUMBER
private static final int STYLE_NUMBER
- See Also:
- Constant Field Values
-
STYLE_CURRENCY
private static final int STYLE_CURRENCY
- See Also:
- Constant Field Values
-
STYLE_PERCENT
private static final int STYLE_PERCENT
- See Also:
- Constant Field Values
-
STYLE_INTEGER
private static final int STYLE_INTEGER
- See Also:
- Constant Field Values
-
-
Method Detail
-
getInstance
public ConversionUtils getInstance()
-
getNumberFormat
public static java.text.NumberFormat getNumberFormat(java.lang.String format, java.util.Locale locale)
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.- Parameters:
format
- the custom or standard formatting pattern to be usedlocale
- theLocale
to be used- Returns:
- an instance of
NumberFormat
- See Also:
NumberFormat
-
getNumberFormat
public static java.text.NumberFormat getNumberFormat(int numberStyle, java.util.Locale locale)
Returns aNumberFormat
instance for the specified number style andLocale
.- Parameters:
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- Returns:
- an instance of
NumberFormat
ornull
if an instance cannot be constructed with the given parameters
-
getNumberStyleAsInt
public static int getNumberStyleAsInt(java.lang.String style)
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.- Parameters:
style
- the string to be checked- Returns:
- the int identifying the style pattern
- See Also:
NumberFormat
-
toNumber
public static java.lang.Number toNumber(java.lang.Object obj)
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.- Parameters:
obj
- - the object to convert
-
toNumber
public static java.lang.Number toNumber(java.lang.Object obj, boolean handleStrings)
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
public static java.lang.Number toNumber(java.lang.String value, java.lang.String format, java.util.Locale locale)
Converts a string to an instance ofNumber
using the specified format andLocale
to parse it.- Parameters:
value
- - the string to convertformat
- - the format the number is inlocale
- - theLocale
- Returns:
- the string as a
Number
ornull
if no conversion is possible - See Also:
NumberFormat.parse(java.lang.String, java.text.ParsePosition)
-
toNumber
public static java.lang.Number toNumber(java.lang.Object value, java.lang.String format, java.util.Locale locale)
Converts an object to an instance ofNumber
using the specified format andLocale
to parse it, if necessary.- Parameters:
value
- - the object to convertformat
- - the format the number is inlocale
- - theLocale
- Returns:
- the object as a
Number
ornull
if no conversion is possible - See Also:
NumberFormat.parse(java.lang.String, java.text.ParsePosition)
-
getDateFormat
public static java.text.DateFormat getDateFormat(java.lang.String format, java.util.Locale locale, java.util.TimeZone timezone)
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.- Parameters:
format
- the custom or standard formatting pattern to be usedlocale
- theLocale
to be usedtimezone
- theTimeZone
to be used- Returns:
- an instance of
DateFormat
- See Also:
SimpleDateFormat
,DateFormat
-
getDateFormat
public static java.text.DateFormat getDateFormat(java.lang.String dateStyle, java.lang.String timeStyle, java.util.Locale locale, java.util.TimeZone timezone)
Returns aDateFormat
instance for the specified date style, time style,Locale
, andTimeZone
.- Parameters:
dateStyle
- the date styletimeStyle
- the time stylelocale
- theLocale
to be usedtimezone
- theTimeZone
to be used- Returns:
- an instance of
DateFormat
- See Also:
getDateFormat(int timeStyle, int dateStyle, Locale locale, TimeZone timezone)
-
getDateFormat
public static java.text.DateFormat getDateFormat(int dateStyle, int timeStyle, java.util.Locale locale, java.util.TimeZone timezone)
Returns aDateFormat
instance for the specified time style, date style,Locale
, andTimeZone
.- Parameters:
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- Returns:
- an instance of
DateFormat
ornull
if an instance cannot be constructed with the given parameters
-
getDateStyleAsInt
public static int getDateStyleAsInt(java.lang.String style)
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.- Parameters:
style
- the string to be checked- Returns:
- the int identifying the style pattern
- See Also:
DateFormat
-
toDate
public static java.util.Date toDate(java.lang.Object obj)
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
.- Parameters:
obj
- - the object to convert
-
toDate
public static java.util.Date toDate(java.lang.Object obj, java.lang.String format, java.util.Locale locale, java.util.TimeZone timezone)
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.- Parameters:
obj
- - the date to convertformat
- - the format the date is inlocale
- - theLocale
timezone
- - theTimeZone
- Returns:
- the object as a
Date
ornull
if no conversion is possible - See Also:
getDateFormat(java.lang.String, java.util.Locale, java.util.TimeZone)
,SimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)
-
toDate
public static java.util.Date toDate(java.lang.String str, java.lang.String format, java.util.Locale locale, java.util.TimeZone timezone)
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.- Parameters:
str
- - the string to parseformat
- - the format the date is inlocale
- - theLocale
timezone
- - theTimeZone
- Returns:
- the string as a
Date
ornull
if the parsing fails - See Also:
getDateFormat(java.lang.String, java.util.Locale, java.util.TimeZone)
,SimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)
-
toCalendar
public static java.util.Calendar toCalendar(java.util.Date date, java.util.Locale locale)
-
toString
public static java.lang.String toString(java.lang.Object value)
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.- Parameters:
value
- the object to be turned into a String- Returns:
- the string value of the object or null if the value is null or it is an array whose first value is null
-
toString
public static java.lang.String toString(java.util.Collection values)
Returns the first value as a String, if any; otherwise returns null.- Parameters:
values
- the Collection to be turned into a string- Returns:
- the string value of the first object in the collection or null if the collection is empty
-
toBoolean
public static java.lang.Boolean toBoolean(java.lang.Object value)
Converts any Object to a boolean usingtoString(Object)
andBoolean.valueOf(String)
.- Parameters:
value
- the object to be converted- Returns:
- a
Boolean
object for the specified value ornull
if the value is null or the conversion failed
-
toLocale
public static java.util.Locale toLocale(java.lang.String value)
Converts a string to aLocale
- Parameters:
value
- - the string to parse- Returns:
- the
Locale
ornull
if the parsing fails
-
toURL
public static java.net.URL toURL(java.lang.String value)
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
.- Parameters:
value
- - the string to parse- Returns:
- the
URL
form of the string ornull
- See Also:
File
,ClassUtils.getResource(String,Object)
,URL
-
toURL
public static java.net.URL toURL(java.lang.String value, java.lang.Object caller)
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
.- Parameters:
value
- - the string to parsecaller
- - the object or Class seeking the url- Returns:
- the
URL
form of the string ornull
- See Also:
File
,ClassUtils.getResource(String,Object)
,URL
-
-