8种数据类型之间的转换

package com.itheima;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class 各种转换 {
	public static void main(String[] args) throws ParseException {
		/*
 * 1.基本数据类型转换
		 */
		//隐式转换 byte,short,char -- int -- long -- float -- double
		//强制转换 
		int a = 12;
		byte b = (byte) a;
		
		/*
 * 2.String StringBuilder
		 */
		//String to StringBuilder
		StringBuilder sb = new StringBuilder("abcde");
		//StringBuilder to String
		String s = sb.toString();
		
		/*
 * 3.String 和  数组
		 */
		   //String to 数组
		String ss = "abcdefg";
		char[] charArray = ss.toCharArray();
		byte[] bytes = ss.getBytes();
		   //数组 to String
		String bys = new String(bytes);
		String chs = new String(charArray);
		
 *4 String 和  基本数据类型
		//基本数据类型 to String
		int an = 10;
		String aa = an+"";
		String aa1 = String.valueOf(an);
		
		//String to  基本数据类型
		int bb = Integer.parseInt("123");
                      //String to int
		char charAt = "123".charAt(0);
                      //String to char
		
		/*
 *5 String 大小写转
		 */
		String bigSmall = "AbCdEf";
		String big = bigSmall.toUpperCase();
		String small = bigSmall.toLowerCase();
		
		/*
 *6 自动装箱和拆箱
		 */
		Integer i = 123;//自动装箱
		int ii = i;		//自动拆箱
		
		/*
 *7 Date 和 String

		Date d = new Date();
		SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = sdf.format(d);
                     //Date to String
		Date parse = sdf.parse(format);
                     //String to Date
	
* 8 Date 和 Calendar
		
		    Date date = new Date();
		Calendar cal = Calendar.getInstance();
		Date time = cal.getTime();
                   //Calendar to Date
		cal.setTime(date);
                   //Date to Calendar

 

你可能感兴趣的:(8种数据类型之间的转换)