String s = "abc";byte[] bytes = s.getBytes();//在IO流的時候用到--- 字節(jié)流往外寫一個字符串的時候
4.3字節(jié)數(shù)組轉(zhuǎn)字符串:
byte [] bys = {97,98,99};String s = new String(bys);System.out.println(s);
4.4字符串轉(zhuǎn)成字符數(shù)組:
String s = "abc";char[] ch = s.toCharArray();//返回值ch是每一個字符組成的數(shù)組 [a,b,c]得到字符串的每一個字符還可以通過charAt()方法遍歷得到
4.5字符串轉(zhuǎn)成基本數(shù)據(jù)類型://以int類型為例
String s = "123";int ps = Integer.parseInt(s);
4.6字符串轉(zhuǎn)成StringBuffer:
1,構(gòu)造函數(shù) String s = "abc"; StringBuffer sb = new StringBuffer(s);//在要用到StringBuffer里面的方法的時候使用,比如反轉(zhuǎn),比如添加2,通過append()方法 String s = "abc"; StringBuffer sb = new StringBuffer(); sb.append(s);
4.7StringBuffer轉(zhuǎn)成字符串:
1,任意類型 + ""2,引用數(shù)據(jù)類型toString()方法3,String.valueOf(參數(shù));參數(shù)傳遞任意類型,返回值是String4,構(gòu)造 StringBuffer sb = new StringBuffer("abc"); String s = new String(sb);//s就是轉(zhuǎn)換后的結(jié)果5,subString StringBuffer sb = new StringBuffer("abc"); String s = sb.subString(0,sb.length());//s就是轉(zhuǎn)換后的結(jié)果
4.8基本數(shù)據(jù)類型與其包裝類之間的手動轉(zhuǎn)換:
int ---> Integer(手動裝箱) int i = 10; Integer ii = new Integer(i);//其中ii就是手動包裝后的引用數(shù)據(jù)類型對象Integer ---> int(手動拆箱) Integer i = new Integer(10); int iv = i.intValue();//iv就是由Integer轉(zhuǎn)換成int得到的值
4.9毫秒值轉(zhuǎn)成日期對象
1,先有一個毫秒值Long類型的d2,轉(zhuǎn)換 Date date = new Date(d); 或者 Date date = new Date(); date.setTime(d);//兩種方法中的date就是將毫秒值轉(zhuǎn)換成的日期對象
4.10日期對象轉(zhuǎn)換成毫秒值
1,先有一個 Date 類型的時間對象2,直接通過getTime的方法獲取毫秒值
4.11將日期對象轉(zhuǎn)換成字符串
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");String s = sdf.format(new Date());//傳遞時間對象,返回格式化后的字符串
4.12將字符串轉(zhuǎn)換成字符串日期對象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");Date d = sdf.parse("2000年10月10日 10:10:10");//傳遞時間對象,返回格式化后的字符串