/**
* 類型轉(zhuǎn)換類,將字符串轉(zhuǎn)換成其他基本類型
*
* @version <tt>Revision: 1.0</tt>
* @author <a href="
mailto:hulk@royasoft.com.cn">hongking</a>
*/
public class TypeConvertor {
public static Integer string2Integer(String str) throws TypeConvertException{
int value = -1;
try{
value = Integer.parseInt(str);
}
catch(Exception e){
throw new TypeConvertException(e);
}
return new Integer(value);
}
public static Long string2Long(String str) throws TypeConvertException{
long value = -1;
try{
value = Long.parseLong(str);
}
catch(Exception e){
throw new TypeConvertException(e);
}
return new Long(value);
}
public static Float string2Float(String str) throws TypeConvertException{
float value = 0;
try{
value = Float.parseFloat(str);
}
catch(Exception e){
throw new TypeConvertException(e);
}
return new Float(value);
}
public static Date string2Date(String str,String format) throws TypeConvertException{
Date dt = null;
try{
if(format == null || "".equals(format)){
DateFormat Format = DateFormat.getDateInstance();
dt = Format.parse(str);
}
else{
SimpleDateFormat sdf = new SimpleDateFormat(format);
dt = sdf.parse(str);
}
}
catch(Exception e){
throw new TypeConvertException(e);
}
return dt;
}
public static Date string2Time(String str,String format) throws TypeConvertException{
Date dt = null;
try{
if(format == null || "".equals(format)){
DateFormat Format = DateFormat.getTimeInstance();
dt = Format.parse(str);
}
else{
SimpleDateFormat sdf = new SimpleDateFormat(format);
dt = sdf.parse(str);
}
}
catch(Exception e){
throw new TypeConvertException(e);
}
return dt;
}
public static Date string2DateTime(String str,String format) throws TypeConvertException{
Date dt = null;
try{
if(format == null || "".equals(format)){
DateFormat Format = DateFormat.getDateTimeInstance();
dt = Format.parse(str);
}
else{
SimpleDateFormat sdf = new SimpleDateFormat(format);
dt = sdf.parse(str);
}
}
catch(Exception e){
throw new TypeConvertException(e);
}
return dt;
}