Package org.javalite.common
Class Convert
java.lang.Object
org.javalite.common.Convert
Convenience class for type conversions.
- Author:
- Igor Polevoy, Eric Nielsen
- 
Method SummaryModifier and TypeMethodDescriptionstatic BigDecimaltoBigDecimal(Object value)Converts value to BigDecimal if it can.static BooleanReturns 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.Blobto bytes array.static DoubleConverts any value toDouble.static FloatConverts value to Float if it can.static IntegerConverts 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 LocalDatetoLocalDate(Object value)Converts a value toLocalDate.static LocalDatetoLocalDate(Date date)static LocalDateTimetoLocalDateTime(long millis)static LocalDateTimetoLocalDateTime(long millis, TimeZone timeZone)static LocalDateTimetoLocalDateTime(Object value)Converts a value toLocalDateTime.static LongConverts value toLongif it can.static ShortConverts value to Short if it can.static DateExpects 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 StringReturns string representation of an object, includingClob.static TimeIf 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 Details- 
toStringReturns 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.
 
- 
toBooleanReturns 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.
 
- 
toSqlDateExpects 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 to- java.sql.Date.
- Returns:
- java.sql.Dateinstance representing input value.
 
- 
truncateToSqlDateExpects 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 to- java.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.
 
- 
truncateToSqlDateThis 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
 
- 
toIsoStringConverts 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
 
- 
toDoubleConverts any value toDouble.- Parameters:
- value- value to convert.
- Returns:
- converted double.
 
- 
toTimeIf 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)
 
- 
toLocalDateConverts 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
- 
toLocalDateTimeConverts 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 to- LocalDateTime.
- timeZone- time zone.
- Returns:
- instance of LocalDateTime.
 
- 
toLocalDateTime- Parameters:
- millis- milliseconds to convert to- LocalDateTime. Uses a default time zone.
- Returns:
- instance of LocalDateTime.
 
- 
toTimestampIf 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)
 
- 
toFloatConverts 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.
 
- 
toLongConverts 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.
 
- 
toIntegerConverts 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.
 
- 
toBigDecimalConverts 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.
 
- 
toBytesConverts 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.
 
- 
toBytesConvertsjava.sql.Blobto bytes array.- Parameters:
- blob- Blob to be converted
- Returns:
- blob converted to byte array
 
- 
toShortConverts 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.
 
 
-