org.javalite.common
Class Convert

java.lang.Object
  extended by org.javalite.common.Convert

public final class Convert
extends Object

Convenience class for type conversions.

Author:
Igor Polevoy, Eric Nielsen

Method Summary
static BigDecimal toBigDecimal(Object value)
          Converts value to BigDecimal if it can.
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'.
static byte[] toBytes(Blob blob)
          Converts java.sql.Blob to bytes array.
static byte[] toBytes(Object value)
          Converts value to bytes array if it can.
static Double toDouble(Object value)
          Converts any value to Double.
static Float toFloat(Object value)
          Converts value to Float if it can.
static Integer toInteger(Object value)
          Converts value to Integer if it can.
static String toIsoString(Date date)
          Converts a java.util.Date to a String in ISO 8601 format: "yyyy-MM-dd'T'HH:mm:ss'Z'" in UTC timezone for timestamps, and "yyyy-MM-dd" for instances of java.sql.Date.
static Long toLong(Object value)
          Converts value to Long if it can.
static Short toShort(Object value)
          Converts value to Short if it can.
static Date toSqlDate(Object value)
          Expects a java.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 toString(Object value)
          Returns string representation of an object, including Clob.
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 using Time.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 using Timestamp.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 a java.sql.Date, java.sql.Timestamp, java.sql.Time, java.util.Date, Long or string with format "yyyy-MM-dd".
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toString

public static String toString(Object value)
Returns string representation of an object, including Clob. For large CLOBs, be careful because this will load an entire CLOB in memory as java.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 a java.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 to java.sql.Date.
Returns:
java.sql.Date instance representing input value.

truncateToSqlDate

public static Date truncateToSqlDate(Object value)
Expects a java.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 to java.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

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 a java.util.Date to a String in ISO 8601 format: "yyyy-MM-dd'T'HH:mm:ss'Z'" in UTC timezone for timestamps, and "yyyy-MM-dd" for instances of java.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 to Double.

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 using Time.valueOf(String). This method might trow IllegalArgumentException if 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 using Timestamp.valueOf(String). This method might trow IllegalArgumentException if 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 to Long if it can. If value is a Long, it is returned, if it is a Number, it is promoted to Long and then returned, if it is a Date, 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 a java.sql.Blob, then data is read from it as bytes. In all other cases the object is converted to String, 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)
Converts java.sql.Blob to 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.


Copyright © 2015 JavaLite. All rights reserved.