在Java项目开发过程中,创建一些公共的方法以实现不同类型数据的转换,小结如下:
/**
* 字符型转化为整型
* @param source
* @return
*/
public static int strtoint(String source){
int num=0;
try{
num=Integer.parseInt(source);
}
catch(Exception e){
e.printStackTrace();num=-1;
}
return num;
}
/**
* 字符型转化为浮点型
* @param source
* @return
*/
public static float strtofloat(String source){
float num=0;
try{
num=Float.parseFloat(source);
}
catch(Exception e){
e.printStackTrace();num=-1;
}
return num;
}
/**
* 整型转化为字符型
* @param source
* @return
*/
public static String inttostr(int source){
String strnum="";
try{
strnum=Integer.toString(source);
}
catch(Exception e){
e.printStackTrace();strnum="";
}
return strnum;
}
/**
* 将时间类型转化为字符型
* @param date
* @return
*/
public static String datetostr(Date date){
String strtime;
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
strtime=format.format(date);
return strtime;
}
实习期间用得最多的上面最后一种方法,关于Java处理时间的问题,颇为杂,有待进一步学习。