常见的一些java类型转化如下,做一些列举,随时更新中
1、String->Date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2008-08-08 12:10:12");
具体请参考JDK API文档
2、String -> int
s="12345";
int i;
第一种方法:i=Integer.parseInt(s);
第二种方法:i=Integer.valueOf(s).intValue();
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?(下边有答案)
3、String ->char
String转char可以先转化为char[],这样在取char
String str = "aadfadsf";
char[] strChar = str.toCharArray();
for (char a:strChar)
System.out.print(a+" ");
}
4、int -> String
int i=12345;
String s="";
第一种方法:s=i+"";
第二种方法:s=String.valueOf(i);
这两种方法有什么区别呢?作用是不是一样的呢?是不是在任何下都能互换呢?
第一种方法:s=i+""; //会产生两个String对象 第一种方法:i=Integer.parseInt(s);//直接使用静态方法,不会产生多余的对象,但会抛出异常 |
5、int ->double
DecimalFormat myformat = new DecimalFormat("#####0.00");//小数位随意定
int a = 9;
Double dd= new Double((double)9);
剩下的见到了随时更新