Java类型转换(详细)

(1)数组 =》 字符串:

Arrays.toString(array);

(2)字符串=》字符数组

 

第一种:String.getChars(srcBegin,srcEnd,destn,destBegin);

第二种:String.toCharArray();

 

(3)字符=》字符串

 

char a[]={‘a’,’b’,’c’};

方法一:System.out.println(new String (a));

方法二:String.valueOf(char c);

 

(4)ArrayList=》字符串

 

ArrayList al=new ArrayList();

al.toString();

 

(5)字符串=》字节数组

 

String str=”李子祥”;

byte b[]=str.getBytes();

 

(6)整形=》字符串

 

String.valueOf(int n);

(7)字符串=》整形

 

Stirng str=”123”;

int a=Integer.parseInt(str);

 

(8)字符串=》float

 

String str=”1.3”;

float a=Float .parseFloat(str);

 

(9)字符串=》日期(java.sql.Date)(只能转换格式为:2017-10-10)

 

Date birth=Date.valueOf(birthStr);

方法二:

字符串=》日期(java.util.Date)

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = sdf.parse(payment_time);

(10)日期格式1=》日期格式2(可以转换任意格式的日期字符串)

 

simpleDateFormat sdf = new simpleDateFormat(“yyyy-MM-dd”);

String date = sdf.format(日期格式1);

java.util.Date time = java.sql.Date.valueOf(date);

 

(11)Integer=》Integer数组

        Integer num = 1001;
        // 转换成字符串
        String str = num.toString();
        // 拆分字符串
        // 定义字符串数组引用和整形引用用于存放取出来的数
        String[] str2 = new String[str.length()];
        Integer[] nums = new Integer[str.length()];
        // 取出来
        for (int i = 0; i < str.length(); i++) {
            // 临时变量接收取出来的每一位数,作为字符串
            String temp = str.substring(i, i + 1);
            // 每次取出来的数重新变成整数
            nums[i] = new Integer(temp);
        }

(12)bigdecimal转换成integer

 
            BigDecimal bd = new BigDecimal(obj);(obj为String类型)
            int intValue = bd.intValue();

(13)double转换int

 
double d = 10.29;
int temp = new Double(d).intValue();

(14)数组转换List

 
String[] split = str.split(cutApartSign);
list = Arrays.asList(split);

(15)byte数组转换String

byte[] a = new byte[]{'','',''}
String str = new String(a);
(16)List转换数组 || Object[]转换String[]
 
ArrayList list = new ArrayList<>();
String[] strings = list.toArray(new String[10]);

 

 

你可能感兴趣的:(java)