Package org.javalite.common
Class Convert
java.lang.Object
org.javalite.common.Convert
Convenience class for type conversions.
- Author:
- Igor Polevoy, Eric Nielsen
-
Method Summary
Modifier and TypeMethodDescriptionstatic BigDecimal
toBigDecimal(Object value)
Converts value to BigDecimal if it can.static Boolean
Returns true if the value is any numeric type and has a value of 1, or if string type has a value of '1', 't', 'y', 'true' or 'yes'.static byte[]
Converts value to bytes array if it can.static byte[]
Convertsjava.sql.Blob
to bytes array.static Double
Converts any value toDouble
.static Float
Converts value to Float if it can.static Integer
Converts value to Integer if it can.static String
toIsoString(Date date)
Converts ajava.util.Date
to aString
in ISO 8601 format: "yyyy-MM-dd'T'HH:mm:ss'Z'" in UTC timezone for timestamps, and "yyyy-MM-dd" for instances ofjava.sql.Date
.static LocalDate
toLocalDate(Object value)
Converts a value toLocalDate
.static LocalDate
toLocalDate(Date date)
static LocalDateTime
toLocalDateTime(long millis)
static LocalDateTime
toLocalDateTime(long millis, TimeZone timeZone)
static LocalDateTime
toLocalDateTime(Object value)
Converts a value toLocalDateTime
.static Long
Converts value toLong
if it can.static Short
Converts value to Short if it can.static Date
Expects ajava.sql.Date
,java.sql.Timestamp
,java.sql.Time
,java.util.Date
,Long
or any object whose toString method has this format:yyyy-mm-dd
.static String
Returns string representation of an object, includingClob
.static Time
If the value is instance of java.sql.Time, returns it, else tries to convert java.util.Date or Long to Time, else tries to convert usingTime.valueOf(String)
.static Timestamp
toTimestamp(Object value)
If the value is instance of java.sql.Timestamp, returns it, else tries to convert java.util.Date or Long to Timestamp, else tries to convert usingTimestamp.valueOf(String)
.static Date
truncateToSqlDate(long time)
This method will truncate hours, minutes, seconds and milliseconds to zeros, to conform with JDBC spec: http://download.oracle.com/javase/6/docs/api/java/sql/Date.html.static Date
truncateToSqlDate(Object value)
Expects ajava.sql.Date
,java.sql.Timestamp
,java.sql.Time
,java.util.Date
,Long
or string with format "yyyy-MM-dd".
-
Method Details
-
toString
Returns string representation of an object, includingClob
. For large CLOBs, be careful because this will load an entire CLOB in memory asjava.lang.String
.- Parameters:
value
- value to convert.- Returns:
- string representation of an object, including
Clob
.
-
toBoolean
Returns true if the value is any numeric type and has a value of 1, or if string type has a value of '1', 't', 'y', 'true' or 'yes'. Otherwise, return false.- Parameters:
value
- value to convert- Returns:
- true if the value is any numeric type and has a value of 1, or if string type has a value of '1', 't', 'y', 'true' or 'yes'. Otherwise, return false.
-
toSqlDate
Expects ajava.sql.Date
,java.sql.Timestamp
,java.sql.Time
,java.util.Date
,Long
or any object whose toString method has this format:yyyy-mm-dd
.- Parameters:
value
- argument that is possible to convert tojava.sql.Date
.- Returns:
java.sql.Date
instance representing input value.
-
truncateToSqlDate
Expects ajava.sql.Date
,java.sql.Timestamp
,java.sql.Time
,java.util.Date
,Long
or string with format "yyyy-MM-dd". This method will truncate hours, minutes, seconds and milliseconds to zeros, to conform with JDBC spec: http://download.oracle.com/javase/6/docs/api/java/sql/Date.html.This method is tread-safe.
- Parameters:
value
- argument that is possible to convert tojava.sql.Date
:java.sql.Date
,java.sql.Timestamp
,java.sql.Time
,java.util.Date
,Long
or any object with toString() ==yyyy-mm-dd
.- Returns:
java.sql.Date
instance representing input value.
-
truncateToSqlDate
This method will truncate hours, minutes, seconds and milliseconds to zeros, to conform with JDBC spec: http://download.oracle.com/javase/6/docs/api/java/sql/Date.html.- Parameters:
time
- time in UTC milliseconds from the epoch- Returns:
- java.sql.Date instance representing time value
-
toIsoString
Converts ajava.util.Date
to aString
in ISO 8601 format: "yyyy-MM-dd'T'HH:mm:ss'Z'" in UTC timezone for timestamps, and "yyyy-MM-dd" for instances ofjava.sql.Date
.This method is tread-safe.
- Parameters:
date
- date to convert- Returns:
- String in ISO 8601 format
-
toDouble
Converts any value toDouble
.- Parameters:
value
- value to convert.- Returns:
- converted double.
-
toTime
If the value is instance of java.sql.Time, returns it, else tries to convert java.util.Date or Long to Time, else tries to convert usingTime.valueOf(String)
. This method might trowIllegalArgumentException
if fails at conversion.- Parameters:
value
- value to convert- Returns:
- instance of java.sql.Time
- See Also:
Time.valueOf(String)
-
toLocalDate
Converts a value toLocalDate
. Tries to convert tojava.util.Date
, then toLocalDate
. If that does not work, tries to convert toString
, then toDate
, and so on.- Parameters:
value
- value to convert- Returns:
- converted LocalDate
-
toLocalDate
-
toLocalDateTime
Converts a value toLocalDateTime
. Tries to convert tojava.util.Date
, then toLocalDateTime
. If that does not work, tries to convert toLong
. If all fails, it tries to parse the value from the string representation of the argument.- Parameters:
value
- value to convert- Returns:
- converted LocalDateTime
-
toLocalDateTime
- Parameters:
millis
- milliseconds to convert toLocalDateTime
.timeZone
- time zone.- Returns:
- instance of
LocalDateTime
.
-
toLocalDateTime
- Parameters:
millis
- milliseconds to convert toLocalDateTime
. Uses a default time zone.- Returns:
- instance of
LocalDateTime
.
-
toTimestamp
If the value is instance of java.sql.Timestamp, returns it, else tries to convert java.util.Date or Long to Timestamp, else tries to convert usingTimestamp.valueOf(String)
. This method might trowIllegalArgumentException
if fails at conversion.- Parameters:
value
- value to convert.- Returns:
- instance of Timestamp.
- See Also:
Timestamp.valueOf(String)
-
toFloat
Converts value to Float if it can. If value is a Float, it is returned, if it is a Number, it is promoted to Float and then returned, in all other cases, it converts the value to String, then tries to parse Float from it.- Parameters:
value
- value to be converted to Float.- Returns:
- value converted to Float.
-
toLong
Converts value toLong
if it can. If value is aLong
, it is returned, if it is aNumber
, it is promoted toLong
and then returned, if it is aDate
, returns its getTime() value, in all other cases, it converts the value to String, then tries to parse Long from it.- Parameters:
value
- value to be converted to Long.- Returns:
- value converted to Long.
-
toInteger
Converts value to Integer if it can. If value is a Integer, it is returned, if it is a Number, it is promoted to Integer and then returned, in all other cases, it converts the value to String, then tries to parse Integer from it.- Parameters:
value
- value to be converted to Integer.- Returns:
- value converted to Integer.
-
toBigDecimal
Converts value to BigDecimal if it can. If value is a BigDecimal, it is returned, if it is a BigDecimal, it is promoted to BigDecimal and then returned, in all other cases, it converts the value to String, then tries to parse BigDecimal from it.- Parameters:
value
- value to be converted to Integer.- Returns:
- value converted to Integer.
-
toBytes
Converts value to bytes array if it can. If the value is byte array, it is simply returned, if it is ajava.sql.Blob
, then data is read from it as bytes. In all other cases the object is converted toString
, then bytes are read from it.- Parameters:
value
- value to be converted.- Returns:
- value converted to byte array.
-
toBytes
Convertsjava.sql.Blob
to bytes array.- Parameters:
blob
- Blob to be converted- Returns:
- blob converted to byte array
-
toShort
Converts value to Short if it can. If value is a Short, it is returned, if it is a Number, it is promoted to Short and then returned, in all other cases, it converts the value to String, then tries to parse Short from it.- Parameters:
value
- value to be converted to Integer.- Returns:
- value converted to Integer.
-