Package org.javalite.common
Class Convert
- java.lang.Object
-
- org.javalite.common.Convert
-
public final class Convert extends Object
Convenience class for type conversions.- Author:
- Igor Polevoy, Eric Nielsen
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static BigDecimaltoBigDecimal(Object value)Converts value to BigDecimal if it can.static BooleantoBoolean(Object value)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[]toBytes(Object value)Converts value to bytes array if it can.static byte[]toBytes(Blob blob)Convertsjava.sql.Blobto bytes array.static DoubletoDouble(Object value)Converts any value toDouble.static FloattoFloat(Object value)Converts value to Float if it can.static IntegertoInteger(Object value)Converts value to Integer if it can.static StringtoIsoString(Date date)Converts ajava.util.Dateto aStringin 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 LongtoLong(Object value)Converts value toLongif it can.static ShorttoShort(Object value)Converts value to Short if it can.static DatetoSqlDate(Object value)Expects ajava.sql.Date,java.sql.Timestamp,java.sql.Time,java.util.Date,Longor any object whose toString method has this format:yyyy-mm-dd.static StringtoString(Object value)Returns string representation of an object, includingClob.static TimetoTime(Object value)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 TimestamptoTimestamp(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 DatetruncateToSqlDate(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 DatetruncateToSqlDate(Object value)Expects ajava.sql.Date,java.sql.Timestamp,java.sql.Time,java.util.Date,Longor string with format "yyyy-MM-dd".
-
-
-
Method Detail
-
toString
public static String toString(Object value)
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
public static Boolean toBoolean(Object value)
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
public static Date toSqlDate(Object value)
Expects ajava.sql.Date,java.sql.Timestamp,java.sql.Time,java.util.Date,Longor 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.Dateinstance representing input value.
-
truncateToSqlDate
public static Date truncateToSqlDate(Object value)
Expects ajava.sql.Date,java.sql.Timestamp,java.sql.Time,java.util.Date,Longor 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,Longor any object with toString() ==yyyy-mm-dd.- Returns:
java.sql.Dateinstance representing input value.
-
truncateToSqlDate
public 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.- Parameters:
time- time in UTC milliseconds from the epoch- Returns:
- java.sql.Date instance representing time value
-
toIsoString
public static String toIsoString(Date date)
Converts ajava.util.Dateto aStringin 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
public static Double toDouble(Object value)
Converts any value toDouble.- Parameters:
value- value to convert.- Returns:
- converted double.
-
toTime
public static Time toTime(Object value)
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 trowIllegalArgumentExceptionif fails at conversion.- Parameters:
value- value to convert- Returns:
- instance of java.sql.Time
- See Also:
Time.valueOf(String)
-
toTimestamp
public 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). This method might trowIllegalArgumentExceptionif fails at conversion.- Parameters:
value- value to convert.- Returns:
- instance of Timestamp.
- See Also:
Timestamp.valueOf(String)
-
toFloat
public static Float toFloat(Object value)
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
public static Long toLong(Object value)
Converts value toLongif it can. If value is aLong, it is returned, if it is aNumber, it is promoted toLongand 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
public static Integer toInteger(Object value)
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
public static BigDecimal toBigDecimal(Object value)
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
public static byte[] toBytes(Object value)
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
public static byte[] toBytes(Blob blob)
Convertsjava.sql.Blobto bytes array.- Parameters:
blob- Blob to be converted- Returns:
- blob converted to byte array
-
toShort
public static Short toShort(Object value)
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.
-
-