/**
* Convert string to Long
*
* @param src string to convert
* Return this value @param def conversion fails
* @return Conversion Good long
*/
public static long Tolong (String src, long def) {
if (src = = null) {
return def;
}
try {
return Long.parselong (SRC);
} catch (Exception ignore) {
}
return def;
}
/**
* Convert string to int
*
* @param src string to convert
* return this value @param def conversion fails
* @return converted int
*/
public static int toint (String src, int def) {
if (src = = null) {
return def;
}
try {
return Integer.parseint (SRC);
} catch (Exception ignore) {
}
return def;
}
/**
* Convert string to byte
*
* @param src string to convert
* return this value @param def conversion fails
* @return converted to a good intbyte
*/
Public static byte ToByte (String src, byte def) {
if (src = = null) {
return def;
}
try {
return Byte.parsebyte (SRC);
} catch (Exception ignore) {
}
return def;
}
/**
* String converted to date
*
* @param date String
* @param pattern Style
* @return Date
*/
public static date ToDate (string date, string pattern) {
if (date = = NULL | | pattern = = NULL) {
return null;
}
try {
SimpleDateFormat format = new SimpleDateFormat (pattern);
return Format.parse (date);
} catch (Exception ignore) {
}
return null;
}
/**
* Split conversion, reserved two decimal places
* @param srcvalue source data
* @return Results
*/
public static double Longtomoney (long Srcvalue) {
BigDecimal obj = new BigDecimal (srcvalue);
Double CNY = obj.divide (new BigDecimal). Doublevalue ();
Return double.parsedouble (String.Format ("%.2f", CNY));
}
Common data type converters in Java