Java里面类型转换总结

时间:2023-03-10 05:02:53
Java里面类型转换总结

1、String 转 int

int i = Integer.valueOf(my_str).intValue();
int i = Integer.parseInt(str);

2、String 转 Integer

Integer integer = Integer.valueOf(str);

3、int 转 String

.) String s = String.valueOf(i);
.) String s = Integer.toString(i);
.) String s = "" + i;

long 转 String:

使用String.valueOf();类似的,可以把int,double等等都转换成String

Long.valueOf(str);还能把String转换为long,不过需要确定是long型

//一、String类方法,String.valueOf(),比如:
long aa = ;
String a = String.valueOf(aa);
//二、最简单的直接将long类型的数据加上一个空串
long aa = ;
String a = aa+"";

4、int 转 Integer

Integer integer = new Integer(i);

5、Integer 转 String

Integer integer=String

6、Integer 转 int

int num=Integer.intValue();

7、String 转 char

char[] ca = "".toCharArray();

8、char 转 String

String s=ca.toString();      //任何类型都可以采用toString()转换成String类型

9、日期

//日期
Calendar calendar=Calendar.getInstance();
int year=calendar.get(Calendar.YEAR);
int month=calendar.get(Calendar.MONTH)+;
int day=calendar.get(Calendar.DATE); //获取今天的日期字符串
String today=java.text.DateFormat.getDateInstance().format(new java.util.Date());
//获取今天的日期
new java.sql.Date(System.currentTimeMillis());

10、JAVA数据类型转换 :

 import java.sql.Date;
public class TypeChange {
public TypeChange() {
}
//change the string type to the int type
public static int stringToInt(String intstr)
{
Integer integer;
integer = Integer.valueOf(intstr);
return integer.intValue();
}
//change int type to the string type
public static String intToString(int value)
{
Integer integer = new Integer(value);
return integer.toString();
}
//change the string type to the float type
public static float stringToFloat(String floatstr)
{
Float floatee;
floatee = Float.valueOf(floatstr);
return floatee.floatValue();
}
//change the float type to the string type
public static String floatToString(float value)
{
Float floatee = new Float(value);
return floatee.toString();
}
//change the string type to the sqlDate type
public static java.sql.Date stringToDate(String dateStr)//转换成时间
{
return java.sql.Date.valueOf(dateStr);
}
//change the sqlDate type to the string type
public static String dateToString(java.sql.Date datee)
{
return datee.toString();
} public static void main(String[] args)
{
java.sql.Date day ;
day = TypeChange.stringToDate("2003-11-3");
String strday = TypeChange.dateToString(day);
System.out.println(strday);
}
}